Skip to content

Commit da89bee

Browse files
committed
wip
1 parent 2d40bed commit da89bee

File tree

13 files changed

+135
-25
lines changed

13 files changed

+135
-25
lines changed

packages/database/src/BelongsTo.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ final class BelongsTo implements Relation
1818
{
1919
public PropertyReflector $property;
2020

21+
public string $name {
22+
get => $this->property->getName();
23+
}
24+
2125
private ?string $parent = null;
2226

2327
public function __construct(

packages/database/src/Builder/QueryBuilders/SelectQueryBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public function __construct(string|object $model, ?ImmutableArray $fields = null
5151
$this->select = new SelectStatement(
5252
table: $this->model->getTableDefinition(),
5353
fields: $fields ?? $this->model
54-
->getSelectFields()
55-
->map(fn (string $fieldName) => new FieldStatement("{$this->model->getTableName()}.{$fieldName}")->withAlias()),
54+
->getSelectFields()
55+
->map(fn (string $fieldName) => new FieldStatement("{$this->model->getTableName()}.{$fieldName}")->withAlias()),
5656
);
5757
}
5858

packages/database/src/HasMany.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ final class HasMany implements Relation
1818
{
1919
public PropertyReflector $property;
2020

21+
public string $name {
22+
get => $this->property->getName();
23+
}
24+
2125
private ?string $parent = null;
2226

2327
public function __construct(

packages/database/src/HasOne.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ final class HasOne implements Relation
1818
{
1919
public PropertyReflector $property;
2020

21+
public string $name {
22+
get => $this->property->getName();
23+
}
24+
2125
private ?string $parent = null;
2226

2327
public function __construct(

packages/database/src/Mappers/SelectModelMapper.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace Tempest\Database\Mappers;
44

5+
use Tempest\Database\BelongsTo;
56
use Tempest\Database\Builder\ModelInspector;
7+
use Tempest\Database\HasMany;
8+
use Tempest\Database\HasOne;
69
use Tempest\Discovery\SkipDiscovery;
710
use Tempest\Mapper\Mapper;
811

@@ -43,39 +46,39 @@ private function normalizeFields(ModelInspector $model, array $rows): array
4346

4447
foreach ($rows as $row) {
4548
foreach ($row as $field => $value) {
46-
$mainField = explode('.', $field)[0];
49+
$parts = explode('.', $field);
50+
51+
$mainField = $parts[0];
4752

4853
// Main fields
4954
if ($mainField === $mainTable) {
50-
$data[substr($field, strlen($mainTable) + 1)] = $value;
55+
$data[$parts[1]] = $value;
5156
continue;
5257
}
5358

54-
// BelongsTo
55-
if ($belongsTo = $model->getBelongsTo($mainField)) {
56-
$data[$belongsTo->property->getName()][str_replace($mainField . '.', '', $field)] = $value;
57-
continue;
58-
}
59+
$relation = $model->getRelation($parts[0]);
60+
61+
// IF count > 2
5962

60-
// HasOne
61-
if ($hasOne = $model->getHasOne($mainField)) {
62-
$data[$hasOne->property->getName()][str_replace($mainField . '.', '', $field)] = $value;
63+
// BelongsTo
64+
if ($relation instanceof BelongsTo || $relation instanceof HasOne) {
65+
$data[$relation->name][$parts[1]] = $value;
6366
continue;
6467
}
6568

6669
// HasMany
67-
if ($hasMany = $model->getHasMany($mainField)) {
68-
$hasManyId = $row[$hasMany->idField()];
70+
if ($relation instanceof HasMany) {
71+
$hasManyId = $row[$relation->idField()];
6972

7073
if ($hasManyId === null) {
7174
// Empty has many relations are initialized it with an empty array
72-
$data[$hasMany->property->getName()] ??= [];
75+
$data[$relation->name] ??= [];
7376
continue;
7477
}
7578

76-
$hasManyRelations[$hasMany->property->getName()] ??= $hasMany;
79+
$hasManyRelations[$relation->name] ??= $relation;
7780

78-
$data[$hasMany->property->getName()][$hasManyId][str_replace($mainField . '.', '', $field)] = $value;
81+
$data[$relation->name][$hasManyId][str_replace($mainField . '.', '', $field)] = $value;
7982
}
8083
}
8184
}

packages/database/src/Relation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
interface Relation extends PropertyAttribute
1010
{
11+
public string $name {
12+
get;
13+
}
14+
1115
public function setParent(string $name): self;
1216

1317
public function getSelectFields(): ImmutableArray;

tests/Fixtures/Migrations/CreateAuthorTable.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Tempest\Database\DatabaseMigration;
88
use Tempest\Database\QueryStatement;
9+
use Tempest\Database\QueryStatements\BelongsToStatement;
910
use Tempest\Database\QueryStatements\CreateTableStatement;
1011
use Tempest\Database\QueryStatements\DropTableStatement;
1112
use Tempest\Database\QueryStatements\PrimaryKeyStatement;
@@ -18,14 +19,11 @@ final class CreateAuthorTable implements DatabaseMigration
1819

1920
public function up(): QueryStatement
2021
{
21-
return new CreateTableStatement(
22-
'authors',
23-
[
24-
new PrimaryKeyStatement(),
25-
new TextStatement('name'),
26-
new TextStatement('type', nullable: true),
27-
],
28-
);
22+
return CreateTableStatement::forModel(Author::class)
23+
->primary()
24+
->text('name')
25+
->text('type', nullable: true)
26+
->belongsTo('authors.publisher_id', 'publishers.id', nullable: true);
2927
}
3028

3129
public function down(): QueryStatement
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Tempest\Fixtures\Migrations;
6+
7+
use Tempest\Database\DatabaseMigration;
8+
use Tempest\Database\QueryStatement;
9+
use Tempest\Database\QueryStatements\CreateTableStatement;
10+
use Tempest\Database\QueryStatements\DropTableStatement;
11+
use Tests\Tempest\Fixtures\Modules\Books\Models\Publisher;
12+
13+
final class CreatePublishersTable implements DatabaseMigration
14+
{
15+
private(set) string $name = '0000-00-00_create_publishers_table';
16+
17+
public function up(): QueryStatement
18+
{
19+
return CreateTableStatement::forModel(Publisher::class)
20+
->primary()
21+
->text('name')
22+
->text('description');
23+
}
24+
25+
public function down(): QueryStatement
26+
{
27+
return DropTableStatement::forModel(Publisher::class);
28+
}
29+
}

tests/Fixtures/Modules/Books/Models/Author.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ public function __construct(
1717

1818
/** @var \Tests\Tempest\Fixtures\Modules\Books\Models\Book[] */
1919
public array $books = [],
20+
public ?Publisher $publisher = null,
2021
) {}
2122
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Fixtures\Modules\Books\Models;
4+
5+
use Tempest\Database\Id;
6+
7+
final class Publisher
8+
{
9+
public Id $id;
10+
11+
public string $name;
12+
13+
public string $description;
14+
}

0 commit comments

Comments
 (0)