Skip to content

Commit 881e580

Browse files
committed
wip
1 parent da89bee commit 881e580

File tree

3 files changed

+70
-40
lines changed

3 files changed

+70
-40
lines changed

packages/database/src/Mappers/SelectModelMapper.php

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,51 +40,81 @@ private function normalizeFields(ModelInspector $model, array $rows): array
4040
{
4141
$data = [];
4242

43-
$mainTable = $model->getTableDefinition()->name;
44-
45-
$hasManyRelations = [];
43+
// $hasManyRelations = [];
4644

4745
foreach ($rows as $row) {
48-
foreach ($row as $field => $value) {
49-
$parts = explode('.', $field);
46+
$row = $this->normalizeRow($model, $row);
47+
48+
foreach ($row as $key => $value) {
49+
if (is_array($value)) {
50+
$data[$key] ??= [];
51+
$data[$key] = [...$data[$key], ...$value];
52+
} else {
53+
$data[$key] = $value;
54+
}
55+
}
56+
}
5057

51-
$mainField = $parts[0];
58+
// foreach ($hasManyRelations as $name => $hasMany) {
59+
// $data[$name] = array_values($data[$name]);
60+
// }
5261

53-
// Main fields
54-
if ($mainField === $mainTable) {
55-
$data[$parts[1]] = $value;
56-
continue;
57-
}
62+
return $data;
63+
}
5864

59-
$relation = $model->getRelation($parts[0]);
65+
public function normalizeRow(ModelInspector $model, array $row): array
66+
{
67+
$mainTable = $model->getTableName();
6068

61-
// IF count > 2
69+
$data = [];
6270

63-
// BelongsTo
64-
if ($relation instanceof BelongsTo || $relation instanceof HasOne) {
65-
$data[$relation->name][$parts[1]] = $value;
66-
continue;
67-
}
71+
foreach ($row as $field => $value) {
72+
$parts = explode('.', $field);
6873

69-
// HasMany
70-
if ($relation instanceof HasMany) {
71-
$hasManyId = $row[$relation->idField()];
74+
$mainField = $parts[0];
7275

73-
if ($hasManyId === null) {
74-
// Empty has many relations are initialized it with an empty array
75-
$data[$relation->name] ??= [];
76-
continue;
77-
}
76+
// Main fields
77+
if ($mainField === $mainTable) {
78+
$data[$parts[1]] = $value;
79+
continue;
80+
}
7881

79-
$hasManyRelations[$relation->name] ??= $relation;
82+
$relation = $model->getRelation($parts[0]);
8083

81-
$data[$relation->name][$hasManyId][str_replace($mainField . '.', '', $field)] = $value;
82-
}
84+
// Nested relations
85+
if (count($parts) > 2) {
86+
$subRelation = model($relation)->getRelation($parts[1]);
87+
88+
$data[$relation->name][$subRelation->name] ??= [];
89+
90+
$data[$relation->name][$subRelation->name] = [
91+
...$data[$relation->name][$subRelation->name],
92+
...$this->normalizeRow(model($subRelation), [
93+
implode('.', array_slice($parts, 1)) => $value,
94+
]),
95+
];
96+
97+
continue;
8398
}
84-
}
8599

86-
foreach ($hasManyRelations as $name => $hasMany) {
87-
$data[$name] = array_values($data[$name]);
100+
// BelongsTo
101+
if ($relation instanceof BelongsTo || $relation instanceof HasOne) {
102+
$data[$relation->name][$parts[1]] = $value;
103+
continue;
104+
}
105+
106+
// HasMany
107+
if ($relation instanceof HasMany) {
108+
$hasManyId = $row[$relation->idField()];
109+
110+
if ($hasManyId === null) {
111+
// Empty has many relations are initialized it with an empty array
112+
$data[$relation->name] ??= [];
113+
continue;
114+
}
115+
116+
$data[$relation->name][$hasManyId][$parts[1]] = $value;
117+
}
88118
}
89119

90120
return $data;

tests/Integration/Database/Builder/InsertQueryBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,17 @@ public function test_insert_on_model_table(): void
7979
$query = query(Author::class)
8080
->insert(
8181
$author,
82-
['name' => 'other name', 'type' => AuthorType::B->value],
82+
['name' => 'other name', 'type' => AuthorType::B->value, 'publisher_id' => null],
8383
)
8484
->build();
8585

8686
$expected = <<<SQL
87-
INSERT INTO `authors` (`name`, `type`)
88-
VALUES (?, ?), (?, ?)
87+
INSERT INTO `authors` (`name`, `type`, `publisher_id`)
88+
VALUES (?, ?, ?), (?, ?, ?)
8989
SQL;
9090

9191
$this->assertSame($expected, $query->toSql());
92-
$this->assertSame(['brent', 'a', 'other name', 'b'], $query->bindings);
92+
$this->assertSame(['brent', 'a', null, 'other name', 'b', null], $query->bindings);
9393
}
9494

9595
public function test_insert_on_model_table_with_new_relation(): void

tests/Integration/Database/Builder/SelectQueryBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function test_select_from_model(): void
7070
$sql = $query->toSql();
7171

7272
$expected = <<<SQL
73-
SELECT authors.name AS `authors.name`, authors.type AS `authors.type`, authors.id AS `authors.id`
73+
SELECT authors.name AS `authors.name`, authors.type AS `authors.type`, authors.publisher_id AS `authors.publisher_id`, authors.id AS `authors.id`
7474
FROM `authors`
7575
SQL;
7676

@@ -271,7 +271,7 @@ public function test_select_first_with_non_object_model(): void
271271
->whereField('id', 2)
272272
->first();
273273

274-
$this->assertSame(['id' => 2, 'name' => 'Other', 'type' => null], $author);
274+
$this->assertSame(['id' => 2, 'name' => 'Other', 'type' => null, 'publisher_id' => null], $author);
275275
}
276276

277277
public function test_select_all_with_non_object_model(): void
@@ -290,7 +290,7 @@ public function test_select_all_with_non_object_model(): void
290290
->all();
291291

292292
$this->assertSame(
293-
[['id' => 2, 'name' => 'Other', 'type' => null], ['id' => 3, 'name' => 'Another', 'type' => 'a']],
293+
[['id' => 2, 'name' => 'Other', 'type' => null, 'publisher_id' => null], ['id' => 3, 'name' => 'Another', 'type' => 'a', 'publisher_id' => null]],
294294
$authors,
295295
);
296296
}
@@ -313,7 +313,7 @@ public function test_with_belongs_to_relation(): void
313313
->build();
314314

315315
$this->assertSame(<<<SQL
316-
SELECT books.title AS `books.title`, books.author_id AS `books.author_id`, books.id AS `books.id`, authors.name AS `authors.name`, authors.type AS `authors.type`, authors.id AS `authors.id`, chapters.title AS `chapters.title`, chapters.contents AS `chapters.contents`, chapters.book_id AS `chapters.book_id`, chapters.id AS `chapters.id`, isbns.value AS `isbns.value`, isbns.book_id AS `isbns.book_id`
316+
SELECT books.title AS `books.title`, books.author_id AS `books.author_id`, books.id AS `books.id`, authors.name AS `authors.name`, authors.type AS `authors.type`, authors.publisher_id AS `authors.publisher_id`, authors.id AS `authors.id`, chapters.title AS `chapters.title`, chapters.contents AS `chapters.contents`, chapters.book_id AS `chapters.book_id`, chapters.id AS `chapters.id`, isbns.value AS `isbns.value`, isbns.book_id AS `isbns.book_id`
317317
FROM `books`
318318
LEFT JOIN authors ON authors.id = books.author_id
319319
LEFT JOIN chapters ON chapters.book_id = books.id

0 commit comments

Comments
 (0)