Skip to content

Commit 0174020

Browse files
committed
wip
1 parent ea70766 commit 0174020

File tree

7 files changed

+46
-22
lines changed

7 files changed

+46
-22
lines changed

packages/database/src/BelongsTo.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ public function getSelectFields(): ImmutableArray
6060
->map(function ($field) use ($relationModel) {
6161
return new FieldStatement(
6262
$relationModel->getTableName() . '.' . $field,
63-
)->withAlias(
64-
sprintf('%s.%s', $this->property->getName(), $field)
65-
)->withAliasPrefix($this->parent);
63+
)
64+
->withAlias(
65+
sprintf('%s.%s', $this->property->getName(), $field),
66+
)
67+
->withAliasPrefix($this->parent);
6668
});
6769
}
6870

packages/database/src/HasMany.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public function getSelectFields(): ImmutableArray
4444
->getSelectFields()
4545
->map(fn ($field) => new FieldStatement(
4646
$relationModel->getTableName() . '.' . $field,
47-
)->withAlias(
48-
sprintf('%s.%s', $this->property->getName(), $field),
49-
)->withAliasPrefix($this->parent));
47+
)
48+
->withAlias(
49+
sprintf('%s.%s', $this->property->getName(), $field),
50+
)
51+
->withAliasPrefix($this->parent));
5052
}
5153

5254
public function primaryKey(): string
@@ -60,7 +62,7 @@ public function idField(): string
6062

6163
return sprintf(
6264
'%s.%s',
63-
$relationModel->getTableName(),
65+
$this->property->getName(),
6466
$relationModel->getPrimaryKey(),
6567
);
6668
}

packages/database/src/HasOne.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ public function getSelectFields(): ImmutableArray
4444
->getSelectFields()
4545
->map(fn ($field) => new FieldStatement(
4646
$relationModel->getTableName() . '.' . $field,
47-
)->withAlias(
48-
sprintf('%s.%s', $this->property->getName(), $field)
49-
)->withAliasPrefix($this->parent));
47+
)
48+
->withAlias(
49+
sprintf('%s.%s', $this->property->getName(), $field),
50+
)
51+
->withAliasPrefix($this->parent));
5052
}
5153

5254
public function getJoinStatement(): JoinStatement

packages/database/src/Mappers/SelectModelMapper.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use Tempest\Database\HasOne;
1010
use Tempest\Discovery\SkipDiscovery;
1111
use Tempest\Mapper\Mapper;
12-
1312
use Tempest\Support\Arr\MutableArray;
13+
1414
use function Tempest\Database\model;
1515
use function Tempest\map;
1616
use function Tempest\Support\arr;
@@ -55,7 +55,7 @@ private function values(ModelInspector $model, array $data): array
5555
foreach ($data as $key => $value) {
5656
$relation = $model->getRelation($key);
5757

58-
if (! $relation instanceof HasMany) {
58+
if (! ($relation instanceof HasMany)) {
5959
continue;
6060
}
6161

@@ -99,9 +99,7 @@ public function normalizeRow(ModelInspector $model, array $row, MutableArray $da
9999
$key .= $relation->name . '.';
100100
$originalKey .= $relation->name . '.';
101101
} elseif ($relation instanceof HasMany) {
102-
$hasManyId = $data->get($key . $relation->idField())
103-
?? $row[$originalKey . $relation->idField()]
104-
?? null;
102+
$hasManyId = $data->get($key . $relation->idField()) ?? $row[$originalKey . $relation->idField()] ?? null;
105103

106104
$originalKey .= $relation->name . '.';
107105

packages/database/src/QueryStatements/FieldStatement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function compile(DatabaseDialect $dialect): string
3333
$aliasPrefix,
3434
str_replace('`', '', $field),
3535
);
36-
} elseif($this->alias) {
36+
} elseif ($this->alias) {
3737
$alias = sprintf(
3838
'`%s%s`',
3939
$aliasPrefix,

packages/database/tests/QueryStatements/SelectStatementTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public function test_select(): void
4949
$this->assertSame($expectedWithBackticks, $statement->compile(DatabaseDialect::POSTGRESQL));
5050

5151
$expectedWithoutBackticks = <<<SQL
52-
SELECT a, b, c, bar.d AS d_alias
53-
FROM foo AS bar
52+
SELECT a, b, c, bar.d AS `d_alias`
53+
FROM `foo` AS `bar`
5454
INNER JOIN foo ON bar.id = foo.id
55-
WHERE foo = "bar"
56-
ORDER BY foo DESC
57-
GROUP BY foo
58-
HAVING foo = "bar"
55+
WHERE `foo` = "bar"
56+
ORDER BY `foo` DESC
57+
GROUP BY `foo`
58+
HAVING `foo` = "bar"
5959
LIMIT 10
6060
OFFSET 100
6161
SQL;

tests/Integration/Database/Mappers/SelectModelMapperTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Integration\Database\Mappers;
44

5+
use Tempest\Auth\Install\User;
56
use Tempest\Database\Mappers\SelectModelMapper;
67
use Tests\Tempest\Fixtures\Modules\Books\Models\Author;
78
use Tests\Tempest\Fixtures\Modules\Books\Models\Book;
@@ -102,6 +103,25 @@ public function test_deeply_nested_has_many_map(): void
102103
$this->assertCount(2, $authors[0]->books[0]->chapters);
103104
}
104105

106+
public function test_map_user_permissions(): void
107+
{
108+
$data = [
109+
[
110+
'users.password' => '$2y$12$sH1n2N7iE6F8RNDceiAHG.WWtXU7j9f2IBwsi3zIb.QMvn0Cwxpi6',
111+
'users.name' => 'Brent',
112+
'users.email' => '[email protected]',
113+
'users.id' => 1,
114+
'userPermissions.user_id' => 1,
115+
'userPermissions.permission_id' => 1,
116+
'userPermissions.id' => 1,
117+
],
118+
];
119+
120+
$users = map($data)->with(SelectModelMapper::class)->to(User::class);
121+
122+
$this->assertCount(1, $users[0]->userPermissions);
123+
}
124+
105125
private function data(): array
106126
{
107127
return [

0 commit comments

Comments
 (0)