Final fixes

This commit is contained in:
Daniil Gentili 2024-03-29 20:39:44 +01:00
parent 8add7e504c
commit 48237a5419
3 changed files with 26 additions and 2 deletions

View File

@ -40,6 +40,7 @@ use function Amp\Future\await;
*/
abstract class DriverArray extends DbArray
{
private bool $inited = false;
/**
* @param Serializer<TValue> $serializer
*/
@ -47,21 +48,26 @@ abstract class DriverArray extends DbArray
protected readonly FieldConfig $config,
protected readonly Serializer $serializer
) {
$this->inited = true;
}
public static function getInstance(FieldConfig $config, DbArray|null $previous): DbArray
{
$migrate = true;
if ($previous !== null
&& $previous::class === static::class
&& $previous->config == $config
) {
if ($previous->inited) {
return $previous;
}
$migrate = false;
}
\assert($config->settings instanceof DriverSettings);
$instance = new static($config, $config->settings->serializer);
if ($previous === null) {
if ($previous === null || !$migrate) {
return $instance;
}

View File

@ -383,7 +383,11 @@ final class OrmTest extends TestCase
$this->assertSame(1, $obj->saveBeforeCnt);
$obj->arr[12345] = 54321;
$obj->arr2[123456] = 654321;
$this->assertSame(54321, $obj->arr[12345]);
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
$obj = $orm[321];
@ -391,6 +395,9 @@ final class OrmTest extends TestCase
$this->assertSame(1, $obj->saveAfterCnt);
$this->assertSame(1, $obj->saveBeforeCnt);
$this->assertSame(54321, $obj->arr[12345]);
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
unset($obj);
$orm = $field->build();
@ -400,6 +407,9 @@ final class OrmTest extends TestCase
$this->assertSame(0, $obj->saveAfterCnt);
$this->assertSame(0, $obj->saveBeforeCnt);
$this->assertSame(54321, $obj->arr[12345]);
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
$orm[321] = $obj;
@ -407,6 +417,9 @@ final class OrmTest extends TestCase
$this->assertSame(0, $obj->saveAfterCnt);
$this->assertSame(0, $obj->saveBeforeCnt);
$this->assertSame(54321, $obj->arr[12345]);
$this->assertSame(654321, $obj->arr2[123456]);
$this->assertCount(1, $obj->arr);
$this->assertCount(1, $obj->arr2);
$f = new ReflectionProperty(ObjectArray::class, 'cache');
$f->getValue($orm)->flushCache();

View File

@ -25,6 +25,11 @@ final class TestObject extends DbObject
ValueType::INT
)]
public DbArray $arr;
#[OrmMappedArray(
KeyType::INT,
ValueType::INT
)]
public DbArray $arr2;
public function __sleep()
{