Skip to content

Commit

Permalink
fix typecasting of object ID for Array_ persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 15, 2022
1 parent 8167d3e commit 79907fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public function tryLoad(Model $model, $id): ?array
$action = $this->action($model, 'select');
$condition = new Model\Scope\Condition('', $id);
$condition->key = $model->getField($model->id_field);
$condition->setOwner($model->createEntity()); // TODO needed for typecasting to apply
$action->filter($condition);
$action->generator->rewind(); // TODO needed for some reasons!

Expand Down
26 changes: 13 additions & 13 deletions tests/ModelNestedArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function atomic(\Closure $fx)
'table' => $mInner,
]);
$m->removeField('id');
$m->id_field = 'name';
$m->id_field = 'birthday';
$m->addField('name');
$m->addField('birthday', ['actual' => 'y', 'type' => 'date']);

Expand Down Expand Up @@ -140,14 +140,14 @@ public function testInsert(): void
['main', Model::HOOK_AFTER_INSERT, []],
['main', Model::HOOK_BEFORE_UNLOAD, []],
['main', Model::HOOK_AFTER_UNLOAD, []],
['main', Model::HOOK_BEFORE_LOAD, ['Karl']],
['main', Model::HOOK_BEFORE_LOAD, [\DateTime::class]],
['main', Model::HOOK_AFTER_LOAD, []],
['main', Model::HOOK_AFTER_SAVE, [false]],
['main', '<<<'],
], $this->hookLog);

$this->assertSame(4, $m->table->loadBy('name', 'Karl')->getId());
$this->assertSameExportUnordered([['Karl']], [[$entity->getId()]]);
$this->assertSameExportUnordered([[new \DateTime('2000-6-1')]], [[$entity->getId()]]);

$this->assertSameExportUnordered([
1 => ['name' => 'John', 'birthday' => new \DateTime('1980-2-1')],
Expand All @@ -160,47 +160,47 @@ public function testUpdate(): void
{
$m = $this->createTestModel();

$m->load('Sue')
$m->load(new \DateTime('2005-4-3'))
->setMulti([
'birthday' => new \DateTime('2020-7-8'),
'name' => 'Susan',
])->save();

$this->assertSame([
['main', Model::HOOK_BEFORE_LOAD, ['Sue']],
['main', Model::HOOK_BEFORE_LOAD, [\DateTime::class]],
['main', Model::HOOK_AFTER_LOAD, []],

['main', '>>>'],
['main', Model::HOOK_VALIDATE, ['save']],
['main', Model::HOOK_BEFORE_SAVE, [true]],
['main', Model::HOOK_BEFORE_UPDATE, [['birthday' => \DateTime::class]]],
['main', Model::HOOK_BEFORE_UPDATE, [['name' => 'Susan']]],
['inner', Model::HOOK_BEFORE_LOAD, [null]],
['inner', Model::HOOK_AFTER_LOAD, []],
['inner', '>>>'],
['inner', Model::HOOK_VALIDATE, ['save']],
['inner', Model::HOOK_BEFORE_SAVE, [true]],
['inner', Model::HOOK_BEFORE_UPDATE, [['y' => \DateTime::class]]],
['inner', Model::HOOK_AFTER_UPDATE, [['y' => \DateTime::class]]],
['inner', Model::HOOK_BEFORE_UPDATE, [['name' => 'Susan']]],
['inner', Model::HOOK_AFTER_UPDATE, [['name' => 'Susan']]],
['inner', Model::HOOK_AFTER_SAVE, [true]],
['inner', '<<<'],
['main', Model::HOOK_AFTER_UPDATE, [['birthday' => \DateTime::class]]],
['main', Model::HOOK_AFTER_UPDATE, [['name' => 'Susan']]],
['main', Model::HOOK_AFTER_SAVE, [true]],
['main', '<<<'],
], $this->hookLog);

$this->assertSameExportUnordered([
1 => ['name' => 'John', 'birthday' => new \DateTime('1980-2-1')],
['name' => 'Sue', 'birthday' => new \DateTime('2020-7-8')],
['name' => 'Susan', 'birthday' => new \DateTime('2005-4-3')],
], $m->export());
}

public function testDelete(): void
{
$m = $this->createTestModel();

$m->delete('Sue');
$m->delete(new \DateTime('2005-4-3'));

$this->assertSame([
['main', Model::HOOK_BEFORE_LOAD, ['Sue']],
['main', Model::HOOK_BEFORE_LOAD, [\DateTime::class]],
['main', Model::HOOK_AFTER_LOAD, []],

['main', '>>>'],
Expand Down

0 comments on commit 79907fe

Please sign in to comment.