Skip to content

Commit a49bd7a

Browse files
committed
Internal field life cycle
1 parent 8c75659 commit a49bd7a

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/Models/Factory/EventBinder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ protected function setProperty($model, string $property, $value): void
3535
$this->getProperty($model, $property)->setValue($model, $value);
3636
}
3737

38+
protected function synchronizeMappedProperties($model): void
39+
{
40+
if (method_exists($model, 'initializeAttributeFields')) {
41+
$model->initializeAttributeFields();
42+
}
43+
}
44+
3845
public function bindPrototype($model)
3946
{
4047
$className = get_class($model);
@@ -57,6 +64,8 @@ public function bindPrototype($model)
5764
$this->setProperty($model, '_relatedObjects', []);
5865
}
5966

67+
$this->synchronizeMappedProperties($model);
68+
6069
return $model;
6170
}
6271

@@ -97,6 +106,8 @@ public function bindRecord($model, array $record = [], bool $isDirty = false, ?b
97106
$this->setProperty($model, '_relatedObjects', []);
98107
}
99108

109+
$this->synchronizeMappedProperties($model);
110+
100111
return $model;
101112
}
102113
}

tests/Divergence/Models/ActiveRecordTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Closure;
1414

1515
use Divergence\Models\Model;
16+
use Divergence\Models\Factory\Instantiator;
17+
use Divergence\Models\Factory\ModelMetadata;
1618

1719
use Divergence\Tests\TestUtils;
1820
use PHPUnit\Framework\TestCase;
@@ -248,6 +250,23 @@ public function testTypedMappedPropertiesCanBeReadDirectlyInsideModelMethods()
248250
$this->assertSame('/my-tag/', $tag->getSlugPath());
249251
}
250252

253+
public function testTypedMappedPropertiesCanBeReadDirectlyInsideModelMethodsAfterFactoryHydration()
254+
{
255+
$instantiator = new Instantiator(new ModelMetadata(Tag::class));
256+
257+
$hydratedTag = $instantiator->instantiateRecord([
258+
'ID' => 1,
259+
'Class' => Tag::class,
260+
'Created' => time(),
261+
'CreatorID' => null,
262+
'Tag' => 'Linux',
263+
'Slug' => 'linux',
264+
]);
265+
266+
$this->assertNotNull($hydratedTag);
267+
$this->assertSame('/linux/', $hydratedTag->getSlugPath());
268+
}
269+
251270
/**
252271
*
253272
*/

0 commit comments

Comments
 (0)