Skip to content

Commit 938c024

Browse files
authored
feat(database): support for hooked virtual properties (#1586)
1 parent a819979 commit 938c024

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

packages/database/src/Builder/ModelInspector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function getSelectFields(): ImmutableArray
347347
continue;
348348
}
349349

350-
if ($property->hasAttribute(Virtual::class)) {
350+
if ($property->isVirtual() || $property->hasAttribute(Virtual::class)) {
351351
continue;
352352
}
353353

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ private function resolveObjectData(object $model): array
398398
continue;
399399
}
400400

401+
if ($property->isVirtual()) {
402+
continue;
403+
}
404+
401405
$propertyName = $property->getName();
402406

403407
if ($property->hasAttribute(Virtual::class)) {

tests/Integration/Database/Builder/IsDatabaseModelTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ public function test_no_result(): void
435435

436436
public function test_create_with_virtual_property(): void
437437
{
438-
$this->migrate(
438+
$this->database->migrate(
439439
CreateMigrationsTable::class,
440440
CreateATable::class,
441441
CreateBTable::class,
@@ -451,6 +451,27 @@ public function test_create_with_virtual_property(): void
451451
$this->assertSame(-$a->id->value, $a->fake);
452452
}
453453

454+
public function test_virtual_hooked_property(): void
455+
{
456+
$this->database->migrate(
457+
CreateMigrationsTable::class,
458+
CreateModelWithHookedVirtualPropertyTable::class,
459+
);
460+
461+
$a = ModelWithHookedVirtualProperty::create(
462+
name: 'a',
463+
);
464+
465+
$this->assertSame('A', $a->hookedName);
466+
467+
$a = ModelWithHookedVirtualProperty::select()->first();
468+
$this->assertSame('A', $a->hookedName);
469+
470+
$a->name = 'b';
471+
$a->save();
472+
$this->assertSame('B', $a->hookedName);
473+
}
474+
454475
public function test_select_virtual_property(): void
455476
{
456477
$this->migrate(
@@ -1047,3 +1068,27 @@ final class BNullableModel
10471068

10481069
public string $name;
10491070
}
1071+
1072+
final class CreateModelWithHookedVirtualPropertyTable implements MigratesUp
1073+
{
1074+
public string $name = '100-create-model-with-hooked-virtual-property';
1075+
1076+
public function up(): QueryStatement
1077+
{
1078+
return new CreateTableStatement('model_with_hooked_virtual_property')
1079+
->primary()
1080+
->string('name');
1081+
}
1082+
}
1083+
1084+
#[Table('model_with_hooked_virtual_property')]
1085+
final class ModelWithHookedVirtualProperty
1086+
{
1087+
use IsDatabaseModel;
1088+
1089+
public string $name;
1090+
1091+
public string $hookedName {
1092+
get => strtoupper($this->name);
1093+
}
1094+
}

0 commit comments

Comments
 (0)