This commit is contained in:
Daniil Gentili 2024-03-29 20:24:38 +01:00
parent 73909971b0
commit 8add7e504c
4 changed files with 33 additions and 12 deletions

View File

@ -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;
}

View File

@ -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
{
}

View File

@ -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();

View File

@ -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++;
}
}