Skip to content

Commit

Permalink
Marking an enty as clean after the afterSave event.
Browse files Browse the repository at this point in the history
By letting the entity be dirty during the event, listeners
can inspect the changes done to the entity during the save process.
I think this is a very important information to have and does not
negatively affect anything else.
  • Loading branch information
lorenzo committed Jul 15, 2015
1 parent 9e35d06 commit 46fbae6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/Table.php
Expand Up @@ -1445,8 +1445,8 @@ protected function _processSave($entity, $options)
['_primary' => false] + $options->getArrayCopy()
);
if ($success || !$options['atomic']) {
$entity->clean();
$this->dispatchEvent('Model.afterSave', compact('entity', 'options'));
$entity->clean();
if (!$options['atomic'] && !$options['_primary']) {
$entity->isNew(false);
$entity->source($this->registryAlias());
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -1888,13 +1888,15 @@ public function testAfterSave()
$called = false;
$listener = function ($e, $entity, $options) use ($data, &$called) {
$this->assertSame($data, $entity);
$this->assertTrue($entity->dirty());
$called = true;
};
$table->eventManager()->on('Model.afterSave', $listener);

$calledAfterCommit = false;
$listenerAfterCommit = function ($e, $entity, $options) use ($data, &$calledAfterCommit) {
$this->assertSame($data, $entity);
$this->assertFalse($entity->dirty());
$calledAfterCommit = true;
};
$table->eventManager()->on('Model.afterSaveCommit', $listenerAfterCommit);
Expand Down

0 comments on commit 46fbae6

Please sign in to comment.