Skip to content

Commit

Permalink
Add '_primary' flag to save options.
Browse files Browse the repository at this point in the history
It's used to ensure 'afterSaveCommit' is not triggered for nested transactional saves.
  • Loading branch information
ADmad committed Feb 26, 2015
1 parent 5025c80 commit 90845c4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,8 @@ public function save(EntityInterface $entity, $options = [])
'atomic' => true,
'associated' => true,
'checkRules' => true,
'checkExisting' => true
'checkExisting' => true,
'_primary' => true
]);

if ($entity->errors()) {
Expand All @@ -1350,7 +1351,9 @@ public function save(EntityInterface $entity, $options = [])
return $this->_processSave($entity, $options);
});
if ($success) {
$this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
if ($options['_primary']) {
$this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
}
$entity->isNew(false);
$entity->source($this->registryAlias());
}
Expand Down Expand Up @@ -1398,7 +1401,7 @@ protected function _processSave($entity, $options)
$this,
$entity,
$options['associated'],
$options->getArrayCopy()
['_primary' => false] + $options->getArrayCopy()
);

if (!$saved && $options['atomic']) {
Expand All @@ -1419,7 +1422,7 @@ protected function _processSave($entity, $options)
$this,
$entity,
$options['associated'],
$options->getArrayCopy()
['_primary' => false] + $options->getArrayCopy()
);
if ($success || !$options['atomic']) {
$entity->clean();
Expand Down
16 changes: 14 additions & 2 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,13 @@ public function testUseBeforeRules()
$table->eventManager()->attach(
function ($event, Entity $entity, \ArrayObject $options, $operation) {
$this->assertEquals(
['atomic' => true, 'associated' => true, 'checkRules' => true, 'checkExisting' => true],
[
'atomic' => true,
'associated' => true,
'checkRules' => true,
'checkExisting' => true,
'_primary' => true
],
$options->getArrayCopy()
);
$this->assertEquals('create', $operation);
Expand Down Expand Up @@ -494,7 +500,13 @@ public function testUseAfterRules()
$table->eventManager()->attach(
function ($event, Entity $entity, \ArrayObject $options, $result, $operation) {
$this->assertEquals(
['atomic' => true, 'associated' => true, 'checkRules' => true, 'checkExisting' => true],
[
'atomic' => true,
'associated' => true,
'checkRules' => true,
'checkExisting' => true,
'_primary' => true
],
$options->getArrayCopy()
);
$this->assertEquals('create', $operation);
Expand Down

0 comments on commit 90845c4

Please sign in to comment.