Browse Source

Add '_primary' flag to save options.

It's used to ensure 'afterSaveCommit' is not triggered for nested transactional saves.
ADmad 11 years ago
parent
commit
90845c44fc
2 changed files with 21 additions and 6 deletions
  1. 7 4
      src/ORM/Table.php
  2. 14 2
      tests/TestCase/ORM/RulesCheckerIntegrationTest.php

+ 7 - 4
src/ORM/Table.php

@@ -1333,7 +1333,8 @@ class Table implements RepositoryInterface, EventListenerInterface
             'atomic' => true,
             'associated' => true,
             'checkRules' => true,
-            'checkExisting' => true
+            'checkExisting' => true,
+            '_primary' => true
         ]);
 
         if ($entity->errors()) {
@@ -1350,7 +1351,9 @@ class Table implements RepositoryInterface, EventListenerInterface
                 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());
             }
@@ -1398,7 +1401,7 @@ class Table implements RepositoryInterface, EventListenerInterface
             $this,
             $entity,
             $options['associated'],
-            $options->getArrayCopy()
+            ['_primary' => false] + $options->getArrayCopy()
         );
 
         if (!$saved && $options['atomic']) {
@@ -1419,7 +1422,7 @@ class Table implements RepositoryInterface, EventListenerInterface
                 $this,
                 $entity,
                 $options['associated'],
-                $options->getArrayCopy()
+                ['_primary' => false] + $options->getArrayCopy()
             );
             if ($success || !$options['atomic']) {
                 $entity->clean();

+ 14 - 2
tests/TestCase/ORM/RulesCheckerIntegrationTest.php

@@ -461,7 +461,13 @@ class RulesCheckerIntegrationTest extends TestCase
         $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);
@@ -494,7 +500,13 @@ class RulesCheckerIntegrationTest extends TestCase
         $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);