Browse Source

Trigger afterDeleteCommit for non-atomic deletes too for primary table.

ADmad 11 years ago
parent
commit
8d47787ad4
2 changed files with 26 additions and 14 deletions
  1. 23 11
      src/ORM/Table.php
  2. 3 3
      tests/TestCase/ORM/TableTest.php

+ 23 - 11
src/ORM/Table.php

@@ -1604,24 +1604,33 @@ class Table implements RepositoryInterface, EventListenerInterface
      */
      */
     public function delete(EntityInterface $entity, $options = [])
     public function delete(EntityInterface $entity, $options = [])
     {
     {
-        $options = new ArrayObject($options + ['atomic' => true, 'checkRules' => true]);
+        $options = new ArrayObject($options + [
+            'atomic' => true,
+            'checkRules' => true,
+            '_primary' => true,
+        ]);
 
 
         $process = function () use ($entity, $options) {
         $process = function () use ($entity, $options) {
             return $this->_processDelete($entity, $options);
             return $this->_processDelete($entity, $options);
         };
         };
 
 
+        $connection = $this->connection();
         if ($options['atomic']) {
         if ($options['atomic']) {
-            $connection = $this->connection();
             $success = $connection->transactional($process);
             $success = $connection->transactional($process);
-            if ($success && !$connection->inTransaction()) {
-                $this->dispatchEvent('Model.afterDeleteCommit', [
-                    'entity' => $entity,
-                    'options' => $options
-                ]);
-            }
-            return $success;
+        } else {
+            $success = $process();
+        }
+
+        if ($success &&
+            !$connection->inTransaction() &&
+            ($options['atomic'] || (!$options['atomic'] && $options['_primary']))
+        ) {
+            $this->dispatchEvent('Model.afterDeleteCommit', [
+                'entity' => $entity,
+                'options' => $options
+            ]);
         }
         }
-        return $process();
+        return $success;
     }
     }
 
 
     /**
     /**
@@ -1661,7 +1670,10 @@ class Table implements RepositoryInterface, EventListenerInterface
             return $event->result;
             return $event->result;
         }
         }
 
 
-        $this->_associations->cascadeDelete($entity, $options->getArrayCopy());
+        $this->_associations->cascadeDelete(
+            $entity,
+            ['_primary' => false] + $options->getArrayCopy()
+        );
 
 
         $query = $this->query();
         $query = $this->query();
         $conditions = (array)$entity->extract($primaryKey);
         $conditions = (array)$entity->extract($primaryKey);

+ 3 - 3
tests/TestCase/ORM/TableTest.php

@@ -2461,7 +2461,7 @@ class TableTest extends TestCase
     public function testDeleteCallbacks()
     public function testDeleteCallbacks()
     {
     {
         $entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);
         $entity = new \Cake\ORM\Entity(['id' => 1, 'name' => 'mark']);
-        $options = new \ArrayObject(['atomic' => true, 'checkRules' => false]);
+        $options = new \ArrayObject(['atomic' => true, 'checkRules' => false, '_primary' => true]);
 
 
         $mock = $this->getMock('Cake\Event\EventManager');
         $mock = $this->getMock('Cake\Event\EventManager');
 
 
@@ -2507,7 +2507,7 @@ class TableTest extends TestCase
     }
     }
 
 
     /**
     /**
-     * Test afterDeleteCommit not called for non-atomic delete
+     * Test afterDeleteCommit is also called for non-atomic delete
      *
      *
      * @return void
      * @return void
      */
      */
@@ -2533,7 +2533,7 @@ class TableTest extends TestCase
 
 
         $table->delete($data, ['atomic' => false]);
         $table->delete($data, ['atomic' => false]);
         $this->assertTrue($called);
         $this->assertTrue($called);
-        $this->assertFalse($calledAfterCommit);
+        $this->assertTrue($calledAfterCommit);
     }
     }
 
 
     /**
     /**