Skip to content

Commit

Permalink
fix nested sql reverse join insert
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Aug 29, 2023
1 parent 69ada6c commit 814b9f3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
36 changes: 34 additions & 2 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,31 @@ protected function beforeInsert(Model $entity, array &$data): void
}
}

/**
* @return list<self>
*/
protected function getReverseJoins(): array
{
$res = [];
foreach ($this->getOwner()->getJoins() as $join) {
if ($join->hasJoin() && $join->getJoin()->shortName === $this->shortName) {
$res[] = $join;
}
}

return $res;
}

/**
* @param mixed $value
*/
protected function setEntityValueAfterUpdate(Model $entity, string $field, $value): void
{
$entity->getField($field); // assert field exists

$entity->getDataRef()[$field] = $value;
}

protected function afterInsert(Model $entity): void
{
if ($this->weak) {
Expand All @@ -541,14 +566,20 @@ protected function afterInsert(Model $entity): void

$this->initSaveBuffer($entity, false);

$foreignId = $entity->getId();
// relies on https://github.com/atk4/data/blob/b3e9ea844e/src/Persistence/Sql/Join.php#L40
$foreignId = $this->hasJoin() ? $entity->get($this->foreignField) : $entity->getId();
$this->assertReferenceIdNotNull($foreignId);

$foreignModel = $this->getForeignModel();
$foreignEntity = $foreignModel->createEntity()
->setMulti($this->getAndUnsetReindexedSaveBuffer($entity))
->set($this->foreignField, $foreignId);
$foreignEntity->save();

foreach ($this->getReverseJoins() as $reverseJoin) {
// relies on https://github.com/atk4/data/blob/b3e9ea844e/src/Persistence/Sql/Join.php#L40
$this->setEntityValueAfterUpdate($entity, $reverseJoin->foreignField, $foreignEntity->get($this->masterField));
}
}

/**
Expand All @@ -567,7 +598,8 @@ protected function beforeUpdate(Model $entity, array &$data): void
}

$foreignModel = $this->getForeignModel();
$foreignId = $this->reverse ? $entity->getId() : $entity->get($this->masterField);
// relies on https://github.com/atk4/data/blob/b3e9ea844e/src/Persistence/Sql/Join.php#L40
$foreignId = $this->reverse ? ($this->hasJoin() ? $entity->get($this->foreignField) : $entity->getId()) : $entity->get($this->masterField);
$this->assertReferenceIdNotNull($foreignId);
$saveBuffer = $this->getAndUnsetReindexedSaveBuffer($entity);
$foreignModel->atomic(function () use ($foreignModel, $foreignId, $saveBuffer) {
Expand Down
14 changes: 12 additions & 2 deletions tests/JoinSqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,21 +575,31 @@ public function testDoubleReverseJoin(): void

$country2 = $country->loadBy('user_name', 'XX');
self::assertSame(2, $country2->getId());
$country2->set('user_name', 'XXx');
$country2->save();

// TODO test save as in testDoubleJoin test
$country2->unload();
self::assertFalse($country2->isLoaded());

self::assertSame($country2->getModel()->getField('contact_id')->getJoin(), $country2->getModel()->getField('contact_phone')->getJoin());

$country->createEntity()->save(['name' => 'LV', 'contact_phone' => '+000', 'user_name' => 'new']);

self::assertSame([
'user' => [
30 => ['id' => 30, 'name' => 'XX', 'contact_id' => 200],
30 => ['id' => 30, 'name' => 'XXx', 'contact_id' => 200],
40 => ['id' => 40, 'name' => 'YYY', 'contact_id' => 300],
['id' => 41, 'name' => 'new', 'contact_id' => 301],
],
'contact' => [
200 => ['id' => 200, 'contact_phone' => '+999', 'country_id' => 2],
300 => ['id' => 300, 'contact_phone' => '+777', 'country_id' => 5],
['id' => 301, 'contact_phone' => '+000', 'country_id' => 6],
],
'country' => [
2 => ['id' => 2, 'name' => 'US'],
5 => ['id' => 5, 'name' => 'India'],
['id' => 6, 'name' => 'LV'],
],
], $this->getDb());
}
Expand Down

0 comments on commit 814b9f3

Please sign in to comment.