Skip to content

Commit

Permalink
Merge pull request #228 from analogueorm/fix/null-foreign-key
Browse files Browse the repository at this point in the history
Make sure related entity objects are in sync
  • Loading branch information
RemiCollin authored Oct 27, 2017
2 parents 907fad0 + 11e564c commit e4b2b35
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/Commands/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function execute()
$mapper->getInstanceCache()->add($entity, $key);
}

$wrappedEntity->unwrap();

return $entity;
}

Expand Down Expand Up @@ -187,6 +189,22 @@ protected function insert()
$id = $this->query->insertGetId($attributes, $sequence);

$aggregate->setEntityAttribute($keyName, $id);

$this->syncAttributes($attributes);
}
}

/**
* Update attributes on actual entity.
*
* @param array $attributes
*
* @return void
*/
protected function syncAttributes(array $attributes)
{
foreach ($attributes as $key => $value) {
$this->aggregate->setEntityAttribute($key, $value);
}
}

Expand All @@ -208,6 +226,7 @@ protected function update()

if (count($dirtyAttributes) > 0) {
$this->query->update($dirtyAttributes);
$this->syncAttributes($dirtyAttributes);
}
}
}
2 changes: 0 additions & 2 deletions tests/cases/Relationships/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,6 @@ public function child(Bar $bar)
$foo->childs->push($barA);
$foo->parents->push($barB);

$this->logQueries();

$mapper = $this->mapper(Foo::class);
$mapper->store($foo);

Expand Down

0 comments on commit e4b2b35

Please sign in to comment.