|
11 | 11 | use Tempest\Database\DatabaseMigration; |
12 | 12 | use Tempest\Database\Exceptions\RelationWasMissing; |
13 | 13 | use Tempest\Database\Exceptions\ValueWasMissing; |
| 14 | +use Tempest\Database\HasMany; |
14 | 15 | use Tempest\Database\HasOne; |
15 | 16 | use Tempest\Database\IsDatabaseModel; |
16 | 17 | use Tempest\Database\Migrations\CreateMigrationsTable; |
@@ -636,6 +637,36 @@ public function test_date_field(): void |
636 | 637 | $this->assertSame('2024-01-01 00:00:00', $model->phpDateTime->format('Y-m-d H:i:s')); |
637 | 638 | $this->assertSame('2024-01-01 00:00:00', $model->tempestDateTime->format('yyyy-MM-dd HH:mm:ss')); |
638 | 639 | } |
| 640 | + |
| 641 | + public function test_model_create_with_has_many_relations(): void |
| 642 | + { |
| 643 | + $this->migrate( |
| 644 | + CreateMigrationsTable::class, |
| 645 | + CreateTestUserMigration::class, |
| 646 | + CreateTestPostMigration::class, |
| 647 | + ); |
| 648 | + |
| 649 | + $user = TestUser::create( |
| 650 | + name: 'Jon', |
| 651 | + posts: [ |
| 652 | + new TestPost('hello', 'world'), |
| 653 | + new TestPost('foo', 'bar'), |
| 654 | + ], |
| 655 | + ); |
| 656 | + |
| 657 | + $this->assertSame('Jon', $user->name); |
| 658 | + $this->assertInstanceOf(PrimaryKey::class, $user->id); |
| 659 | + |
| 660 | + $posts = TestPost::select() |
| 661 | + ->where('testuser_id', $user->id->value) |
| 662 | + ->all(); |
| 663 | + |
| 664 | + $this->assertCount(2, $posts); |
| 665 | + $this->assertSame('hello', $posts[0]->title); |
| 666 | + $this->assertSame('world', $posts[0]->body); |
| 667 | + $this->assertSame('foo', $posts[1]->title); |
| 668 | + $this->assertSame('bar', $posts[1]->body); |
| 669 | + } |
639 | 670 | } |
640 | 671 |
|
641 | 672 | final class Foo |
@@ -983,3 +1014,66 @@ public function __construct( |
983 | 1014 | public ?ChildModel $child2 = null, |
984 | 1015 | ) {} |
985 | 1016 | } |
| 1017 | + |
| 1018 | +final class TestUser |
| 1019 | +{ |
| 1020 | + use IsDatabaseModel; |
| 1021 | + |
| 1022 | + public PrimaryKey $id; |
| 1023 | + |
| 1024 | + /** @var \Tests\Tempest\Integration\Database\Builder\TestPost[] */ |
| 1025 | + #[HasMany] |
| 1026 | + public array $posts = []; |
| 1027 | + |
| 1028 | + public function __construct( |
| 1029 | + public string $name, |
| 1030 | + ) {} |
| 1031 | +} |
| 1032 | + |
| 1033 | +final class TestPost |
| 1034 | +{ |
| 1035 | + use IsDatabaseModel; |
| 1036 | + |
| 1037 | + public PrimaryKey $id; |
| 1038 | + |
| 1039 | + public function __construct( |
| 1040 | + public string $title, |
| 1041 | + public string $body, |
| 1042 | + ) {} |
| 1043 | +} |
| 1044 | + |
| 1045 | +final class CreateTestUserMigration implements DatabaseMigration |
| 1046 | +{ |
| 1047 | + public string $name = '010_create_test_users'; |
| 1048 | + |
| 1049 | + public function up(): QueryStatement |
| 1050 | + { |
| 1051 | + return CreateTableStatement::forModel(TestUser::class) |
| 1052 | + ->primary() |
| 1053 | + ->text('name'); |
| 1054 | + } |
| 1055 | + |
| 1056 | + public function down(): ?QueryStatement |
| 1057 | + { |
| 1058 | + return null; |
| 1059 | + } |
| 1060 | +} |
| 1061 | + |
| 1062 | +final class CreateTestPostMigration implements DatabaseMigration |
| 1063 | +{ |
| 1064 | + public string $name = '011_create_test_posts'; |
| 1065 | + |
| 1066 | + public function up(): QueryStatement |
| 1067 | + { |
| 1068 | + return CreateTableStatement::forModel(TestPost::class) |
| 1069 | + ->primary() |
| 1070 | + ->belongsTo('test_posts.testuser_id', 'test_users.id') |
| 1071 | + ->string('title') |
| 1072 | + ->text('body'); |
| 1073 | + } |
| 1074 | + |
| 1075 | + public function down(): ?QueryStatement |
| 1076 | + { |
| 1077 | + return null; |
| 1078 | + } |
| 1079 | +} |
0 commit comments