Skip to content

Commit 900a87d

Browse files
committed
wip
1 parent c3a816d commit 900a87d

File tree

4 files changed

+111
-4
lines changed

4 files changed

+111
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Tempest\Database\Builder\ModelDefinition;
1010
use Tempest\Database\Builder\TableDefinition;
1111
use Tempest\Database\Id;
12+
use Tempest\Database\Mappers\DatabaseModelMapper;
1213
use Tempest\Database\Query;
1314
use Tempest\Database\QueryStatements\JoinStatement;
1415
use Tempest\Database\QueryStatements\OrderByStatement;
@@ -65,7 +66,10 @@ public function first(mixed ...$bindings): mixed
6566
return $query->fetchFirst();
6667
}
6768

68-
$result = map($query)->collection()->to($this->modelClass);
69+
$result = map($query)
70+
->collection()
71+
->with(DatabaseModelMapper::class)
72+
->to($this->modelClass);
6973

7074
if ($result === []) {
7175
return null;

packages/database/src/Mappers/QueryToModelMapper.php renamed to packages/database/src/Mappers/DatabaseModelMapper.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
namespace Tempest\Database\Mappers;
66

77
use Tempest\Database\Builder\ModelDefinition;
8-
use Tempest\Database\Query;
98
use Tempest\Mapper\CasterFactory;
109
use Tempest\Mapper\Mapper;
1110
use Tempest\Reflection\ClassReflector;
1211
use Tempest\Reflection\PropertyReflector;
1312

14-
final readonly class QueryToModelMapper implements Mapper
13+
// TODO: refactor
14+
final readonly class DatabaseModelMapper implements Mapper
1515
{
1616
public function __construct(
1717
private CasterFactory $casterFactory,
1818
) {}
1919

2020
public function canMap(mixed $from, mixed $to): bool
2121
{
22-
return $from instanceof Query;
22+
return false;
2323
}
2424

2525
public function map(mixed $from, mixed $to): array
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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\Book;
12+
use Tests\Tempest\Fixtures\Modules\Books\Models\Isbn;
13+
14+
final class CreateIsbnTable implements DatabaseMigration
15+
{
16+
private(set) string $name = '0000-00-00_create_isbns_table';
17+
18+
public function up(): QueryStatement
19+
{
20+
return CreateTableStatement::forModel(Isbn::class)
21+
->primary()
22+
->text('value')
23+
->belongsTo('isbns.book_id', 'books.id');
24+
}
25+
26+
public function down(): QueryStatement
27+
{
28+
return DropTableStatement::forModel(Book::class);
29+
}
30+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Integration\Database\Mappers;
4+
5+
use Tests\Tempest\Fixtures\Migrations\CreateIsbnTable;
6+
use Tempest\Database\Migrations\CreateMigrationsTable;
7+
use Tests\Tempest\Fixtures\Migrations\CreateAuthorTable;
8+
use Tests\Tempest\Fixtures\Migrations\CreateBookTable;
9+
use Tests\Tempest\Fixtures\Migrations\CreateChapterTable;
10+
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
11+
use function Tempest\Database\query;
12+
13+
final class DatabaseModelMapperTest extends FrameworkIntegrationTestCase
14+
{
15+
public function test_map(): void
16+
{
17+
$this->seed();
18+
19+
$query = query('books')
20+
->select(
21+
'books.title',
22+
'authors.name',
23+
)
24+
->build()
25+
;
26+
}
27+
28+
private function seed(): void
29+
{
30+
$this->migrate(
31+
CreateMigrationsTable::class,
32+
CreateAuthorTable::class,
33+
CreateBookTable::class,
34+
CreateChapterTable::class,
35+
CreateIsbnTable::class,
36+
);
37+
38+
query('authors')->insert(
39+
['name' => 'Brent'],
40+
['name' => 'Tolkien'],
41+
)->execute();
42+
43+
query('books')->insert(
44+
['title' => 'LOTR 1', 'author_id' => 2],
45+
['title' => 'LOTR 2', 'author_id' => 2],
46+
['title' => 'LOTR 3', 'author_id' => 2],
47+
['title' => 'Timeline Taxi', 'author_id' => 1],
48+
)->execute();
49+
50+
query('isbns')->insert(
51+
['value' => 'lotr-1', 'book_id' => 1],
52+
['value' => 'lotr-2', 'book_id' => 2],
53+
['value' => 'lotr-3', 'book_id' => 3],
54+
['value' => 'tt', 'book_id' => 4],
55+
)->execute();
56+
57+
query('chapters')->insert(
58+
['title' => 'LOTR 1.1', 'book_id' => 1],
59+
['title' => 'LOTR 1.2', 'book_id' => 1],
60+
['title' => 'LOTR 1.3', 'book_id' => 1],
61+
['title' => 'LOTR 2.1', 'book_id' => 2],
62+
['title' => 'LOTR 2.2', 'book_id' => 2],
63+
['title' => 'LOTR 2.3', 'book_id' => 2],
64+
['title' => 'LOTR 3.1', 'book_id' => 3],
65+
['title' => 'LOTR 3.2', 'book_id' => 3],
66+
['title' => 'LOTR 3.3', 'book_id' => 3],
67+
['title' => 'Timeline Taxi Chapter 1', 'book_id' => 4],
68+
['title' => 'Timeline Taxi Chapter 2', 'book_id' => 4],
69+
['title' => 'Timeline Taxi Chapter 3', 'book_id' => 4],
70+
['title' => 'Timeline Taxi Chapter 4', 'book_id' => 4],
71+
)->execute();
72+
}
73+
}

0 commit comments

Comments
 (0)