From 8add7e504cbc70bd6ce0e43eb78002fd06babd34 Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 29 Mar 2024 20:24:38 +0100 Subject: [PATCH] Fixes --- src/DbAutoProperties.php | 8 ++++++-- src/DbObject.php | 11 +++-------- tests/OrmTest.php | 6 ++++++ tests/TestObject.php | 20 ++++++++++++++++++-- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/DbAutoProperties.php b/src/DbAutoProperties.php index 23536ad..6922261 100644 --- a/src/DbAutoProperties.php +++ b/src/DbAutoProperties.php @@ -75,8 +75,12 @@ trait DbAutoProperties ); $promises[] = async(function () use ($config, $property) { - $v = $config->build($property->getValue()); - $property->setValue($v); + $v = $config->build( + $property->isInitialized($this) + ? $property->getValue($this) + : null + ); + $property->setValue($this, $v); if ($v instanceof CachedArray) { $this->properties []= $v; } diff --git a/src/DbObject.php b/src/DbObject.php index 2548c51..243db15 100644 --- a/src/DbObject.php +++ b/src/DbObject.php @@ -31,16 +31,11 @@ abstract class DbObject * * @internal Do not invoke manually. */ - final public function initDb(ObjectContainer $mapper, string|int $key): void + final public function initDb(ObjectContainer $mapper, string|int $key, FieldConfig $config): void { - if (isset($this->key)) { - $this->mapper = $mapper; - $this->key = $key; - return; - } $this->mapper = $mapper; $this->key = $key; - $this->onLoaded(); + $this->onLoaded($config); } /** @@ -57,7 +52,7 @@ abstract class DbObject /** * Method invoked after loading the object. */ - protected function onLoaded(): void + protected function onLoaded(FieldConfig $config): void { } diff --git a/tests/OrmTest.php b/tests/OrmTest.php index 919981a..fb33d35 100644 --- a/tests/OrmTest.php +++ b/tests/OrmTest.php @@ -382,11 +382,15 @@ final class OrmTest extends TestCase $this->assertSame(1, $obj->saveAfterCnt); $this->assertSame(1, $obj->saveBeforeCnt); + $obj->arr[12345] = 54321; + $this->assertSame(54321, $obj->arr[12345]); + $obj = $orm[321]; $this->assertSame(1, $obj->loadedCnt); $this->assertSame(1, $obj->saveAfterCnt); $this->assertSame(1, $obj->saveBeforeCnt); + $this->assertSame(54321, $obj->arr[12345]); unset($obj); $orm = $field->build(); @@ -395,12 +399,14 @@ final class OrmTest extends TestCase $this->assertSame(1, $obj->loadedCnt); $this->assertSame(0, $obj->saveAfterCnt); $this->assertSame(0, $obj->saveBeforeCnt); + $this->assertSame(54321, $obj->arr[12345]); $orm[321] = $obj; $this->assertSame(1, $obj->loadedCnt); $this->assertSame(0, $obj->saveAfterCnt); $this->assertSame(0, $obj->saveBeforeCnt); + $this->assertSame(54321, $obj->arr[12345]); $f = new ReflectionProperty(ObjectArray::class, 'cache'); $f->getValue($orm)->flushCache(); diff --git a/tests/TestObject.php b/tests/TestObject.php index 170d9a5..d3551a0 100644 --- a/tests/TestObject.php +++ b/tests/TestObject.php @@ -2,23 +2,38 @@ namespace danog\AsyncOrm\Test; +use danog\AsyncOrm\Annotations\OrmMappedArray; +use danog\AsyncOrm\DbArray; +use danog\AsyncOrm\DbAutoProperties; use danog\AsyncOrm\DbObject; +use danog\AsyncOrm\FieldConfig; +use danog\AsyncOrm\KeyType; +use danog\AsyncOrm\ValueType; final class TestObject extends DbObject { + use DbAutoProperties; + public int $loadedCnt = 0; public int $saveAfterCnt = 0; public int $saveBeforeCnt = 0; public mixed $savedProp = null; + #[OrmMappedArray( + KeyType::INT, + ValueType::INT + )] + public DbArray $arr; + public function __sleep() { - return ['savedProp']; + return ['savedProp', 'arr']; } - protected function onLoaded(): void + protected function onLoaded(FieldConfig $config): void { + $this->initDbProperties($config->settings, $config->table); $this->loadedCnt++; } protected function onAfterSave(): void @@ -27,6 +42,7 @@ final class TestObject extends DbObject } protected function onBeforeSave(): void { + $this->saveDbProperties(); $this->saveBeforeCnt++; } }