Skip to content

Commit a59bc44

Browse files
committed
wip
1 parent a6a9aac commit a59bc44

File tree

6 files changed

+19
-111
lines changed

6 files changed

+19
-111
lines changed

packages/database/src/Builder/ModelInspector.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Tempest\Database\BelongsTo;
77
use Tempest\Database\Config\DatabaseConfig;
88
use Tempest\Database\Eager;
9-
use Tempest\Database\Exceptions\ModelDidNotHavePrimaryColumn;
10-
use Tempest\Database\Exceptions\ModelHadMultiplePrimaryColumns;
119
use Tempest\Database\HasMany;
1210
use Tempest\Database\HasOne;
1311
use Tempest\Database\PrimaryKey;
@@ -311,7 +309,7 @@ public function isRelationLoaded(string|PropertyReflector|Relation $relation): b
311309
return false;
312310
}
313311

314-
if (! $relation instanceof Relation) {
312+
if (! ($relation instanceof Relation)) {
315313
$relation = $this->getRelation($relation);
316314
}
317315

@@ -327,7 +325,7 @@ public function isRelationLoaded(string|PropertyReflector|Relation $relation): b
327325
return false;
328326
}
329327

330-
return true;
328+
return true;
331329
}
332330

333331
public function getSelectFields(): ImmutableArray
@@ -338,6 +336,10 @@ public function getSelectFields(): ImmutableArray
338336

339337
$selectFields = arr();
340338

339+
if ($primaryKey = $this->getPrimaryKeyProperty()) {
340+
$selectFields[] = $primaryKey->getName();
341+
}
342+
341343
foreach ($this->reflector->getPublicProperties() as $property) {
342344
$relation = $this->getRelation($property->getName());
343345

@@ -360,10 +362,6 @@ public function getSelectFields(): ImmutableArray
360362
}
361363
}
362364

363-
if ($primaryKey = $this->getPrimaryKeyProperty()) {
364-
$selectFields[] = $primaryKey->getName();
365-
}
366-
367365
return $selectFields;
368366
}
369367

@@ -509,10 +507,6 @@ public function getPrimaryKeyProperty(): ?PropertyReflector
509507
return match ($primaryKeys->count()) {
510508
0 => null,
511509
default => $primaryKeys->first(),
512-
// default => throw ModelHadMultiplePrimaryColumns::found(
513-
// model: $this->model,
514-
// properties: $primaryKeys->map(fn (PropertyReflector $property) => $property->getName())->toArray(),
515-
// ),
516510
};
517511
}
518512

packages/database/src/Exceptions/ModelHadMultiplePrimaryColumns.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/database/src/IsDatabaseModel.php

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
use Tempest\Reflection\PropertyReflector;
1414
use Tempest\Validation\SkipValidation;
1515

16+
use function Tempest\Support\arr;
17+
use function Tempest\Support\str;
18+
1619
trait IsDatabaseModel
1720
{
1821
#[SkipValidation]
@@ -171,11 +174,8 @@ public function refresh(): self
171174
{
172175
$model = inspect($this);
173176

174-
if (! $model->hasPrimaryKey()) {
175-
throw Exceptions\ModelDidNotHavePrimaryColumn::neededForMethod($this, 'refresh');
176-
}
177-
178-
$loadedRelations = $model->getRelations()
177+
$loadedRelations = $model
178+
->getRelations()
179179
->filter(fn (Relation $relation) => $model->isRelationLoaded($relation));
180180

181181
$primaryKeyProperty = $model->getPrimaryKeyProperty();
@@ -192,7 +192,7 @@ public function refresh(): self
192192
);
193193
}
194194

195-
foreach ($model->getValueFields() as $property) {
195+
foreach ($model->getValueFields() as $property) {
196196
$property->setValue(
197197
object: $this,
198198
value: $property->getValue($new),
@@ -209,21 +209,17 @@ public function load(string ...$relations): self
209209
{
210210
$model = inspect($this);
211211

212-
if (! $model->hasPrimaryKey()) {
213-
throw Exceptions\ModelDidNotHavePrimaryColumn::neededForMethod($this, 'load');
214-
}
215-
216212
$primaryKeyProperty = $model->getPrimaryKeyProperty();
217213
$primaryKeyValue = $primaryKeyProperty->getValue($this);
218214

219215
$new = self::get($primaryKeyValue, $relations);
220216

221-
foreach (new ClassReflector($new)->getPublicProperties() as $property) {
222-
if (! in_array($property->getName(), $relations, strict: true)) {
223-
continue;
224-
}
217+
$fieldsToUpdate = arr($relations)
218+
->map(fn (string $relation) => str($relation)->before('.')->toString())
219+
->unique();
225220

226-
$property->setValue($this, $property->getValue($new));
221+
foreach ($fieldsToUpdate as $fieldToUpdate) {
222+
$this->{$fieldToUpdate} = $new->{$fieldToUpdate};
227223
}
228224

229225
return $this;
@@ -277,10 +273,6 @@ public function update(mixed ...$params): self
277273
{
278274
$model = inspect($this);
279275

280-
if (! $model->hasPrimaryKey()) {
281-
throw Exceptions\ModelDidNotHavePrimaryColumn::neededForMethod($this, 'update');
282-
}
283-
284276
$model->validate(...$params);
285277

286278
query($this)

tests/Integration/Database/Builder/CustomPrimaryKeyTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace Tests\Tempest\Integration\Database\Builder;
44

55
use Tempest\Database\DatabaseMigration;
6-
use Tempest\Database\Exceptions\ModelHadMultiplePrimaryColumns;
76
use Tempest\Database\Migrations\CreateMigrationsTable;
87
use Tempest\Database\PrimaryKey;
98
use Tempest\Database\QueryStatement;
109
use Tempest\Database\QueryStatements\CreateTableStatement;
1110
use Tests\Tempest\Integration\FrameworkIntegrationTestCase;
1211

13-
use function Tempest\Database\inspect;
1412
use function Tempest\Database\query;
1513

1614
final class CustomPrimaryKeyTest extends FrameworkIntegrationTestCase
@@ -47,16 +45,6 @@ public function test_update_or_create_with_custom_primary_key(): void
4745
$this->assertSame('Advanced Time Magic', $updated->magic);
4846
}
4947

50-
public function test_model_with_multiple_id_properties_throws_exception(): void
51-
{
52-
$this->expectException(ModelHadMultiplePrimaryColumns::class);
53-
$this->expectExceptionMessage(
54-
'`Tests\Tempest\Integration\Database\Builder\ModelWithMultipleIds` has multiple `Id` properties (uuid and external_id). Only one `Id` property is allowed per model.',
55-
);
56-
57-
inspect(ModelWithMultipleIds::class)->getPrimaryKey();
58-
}
59-
6048
public function test_model_without_id_property_still_works(): void
6149
{
6250
$this->migrate(CreateMigrationsTable::class, CreateModelWithoutIdMigration::class);

tests/Integration/Database/Builder/IsDatabaseModelTest.php

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -585,47 +585,6 @@ public function test_delete_via_model_instance_with_primary_key(): void
585585
$this->assertSame('second', Foo::get($foo2->id)->bar);
586586
}
587587

588-
public function test_delete_via_model_instance_without_primary_key(): void
589-
{
590-
$this->migrate(
591-
CreateMigrationsTable::class,
592-
CreateModelWithoutPrimaryKeyMigration::class,
593-
);
594-
595-
$model = new ModelWithoutPrimaryKey(name: 'Frieren', description: 'Elf mage');
596-
$model->save();
597-
598-
$this->expectException(DeleteStatementWasInvalid::class);
599-
$model->delete();
600-
}
601-
602-
public function test_delete_via_model_class_without_primary_key(): void
603-
{
604-
$this->migrate(
605-
CreateMigrationsTable::class,
606-
CreateModelWithoutPrimaryKeyMigration::class,
607-
);
608-
609-
query(ModelWithoutPrimaryKey::class)->create(name: 'Himmel', description: 'Hero');
610-
query(ModelWithoutPrimaryKey::class)->create(name: 'Heiter', description: 'Priest');
611-
query(ModelWithoutPrimaryKey::class)->create(name: 'Eisen', description: 'Warrior');
612-
613-
$this->assertCount(3, query(ModelWithoutPrimaryKey::class)->select()->all());
614-
615-
query(ModelWithoutPrimaryKey::class)
616-
->delete()
617-
->where('name', 'Himmel')
618-
->execute();
619-
620-
$remaining = query(ModelWithoutPrimaryKey::class)->select()->all();
621-
$this->assertCount(2, $remaining);
622-
623-
$names = array_map(fn (ModelWithoutPrimaryKey $model) => $model->name, $remaining);
624-
$this->assertContains('Heiter', $names);
625-
$this->assertContains('Eisen', $names);
626-
$this->assertNotContains('Himmel', $names);
627-
}
628-
629588
public function test_delete_with_uninitialized_primary_key(): void
630589
{
631590
$this->migrate(
@@ -973,8 +932,6 @@ final class StaticMethodTableNameModel
973932
use IsDatabaseModel;
974933
}
975934

976-
977-
978935
#[Table('through')]
979936
final class ThroughModel
980937
{
@@ -1049,8 +1006,6 @@ public function down(): ?QueryStatement
10491006

10501007
final class ModelWithoutPrimaryKey
10511008
{
1052-
use IsDatabaseModel;
1053-
10541009
public function __construct(
10551010
public string $name,
10561011
public string $description,

tests/Integration/Database/ModelInspector/HasOneTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public function test_has_one_throws_exception_for_model_without_primary_key(): v
103103
#[Table('relation')]
104104
final class HasOneTestRelationModel
105105
{
106+
public PrimaryKey $id;
107+
106108
#[HasOne]
107109
public HasOneTestOwnerModel $owner;
108110

0 commit comments

Comments
 (0)