浏览代码

Use Connection::isTransaction() instead of _primary flag.

The former is a more robust way to check if transaction is running
and even accounts for transactions started outside Table::save().
ADmad 11 年之前
父节点
当前提交
451dc357e9
共有 3 个文件被更改,包括 29 次插入6 次删除
  1. 3 4
      src/ORM/Table.php
  2. 0 2
      tests/TestCase/ORM/RulesCheckerIntegrationTest.php
  3. 26 0
      tests/TestCase/ORM/TableTest.php

+ 3 - 4
src/ORM/Table.php

@@ -1334,7 +1334,6 @@ class Table implements RepositoryInterface, EventListenerInterface
             'associated' => true,
             'checkRules' => true,
             'checkExisting' => true,
-            '_primary' => true
         ]);
 
         if ($entity->errors()) {
@@ -1351,7 +1350,7 @@ class Table implements RepositoryInterface, EventListenerInterface
                 return $this->_processSave($entity, $options);
             });
             if ($success) {
-                if ($options['_primary']) {
+                if (!$connection->inTransaction()) {
                     $this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
                 }
                 $entity->isNew(false);
@@ -1401,7 +1400,7 @@ class Table implements RepositoryInterface, EventListenerInterface
             $this,
             $entity,
             $options['associated'],
-            ['_primary' => false] + $options->getArrayCopy()
+            $options->getArrayCopy()
         );
 
         if (!$saved && $options['atomic']) {
@@ -1422,7 +1421,7 @@ class Table implements RepositoryInterface, EventListenerInterface
                 $this,
                 $entity,
                 $options['associated'],
-                ['_primary' => false] + $options->getArrayCopy()
+                $options->getArrayCopy()
             );
             if ($success || !$options['atomic']) {
                 $entity->clean();

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

@@ -466,7 +466,6 @@ class RulesCheckerIntegrationTest extends TestCase
                         'associated' => true,
                         'checkRules' => true,
                         'checkExisting' => true,
-                        '_primary' => true
                     ],
                     $options->getArrayCopy()
                 );
@@ -505,7 +504,6 @@ class RulesCheckerIntegrationTest extends TestCase
                         'associated' => true,
                         'checkRules' => true,
                         'checkExisting' => true,
-                        '_primary' => true
                     ],
                     $options->getArrayCopy()
                 );

+ 26 - 0
tests/TestCase/ORM/TableTest.php

@@ -1812,6 +1812,32 @@ class TableTest extends TestCase
     }
 
     /**
+     * Asserts the afterSaveCommit is not triggered if transaction is running.
+     *
+     * @return [type]
+     */
+    public function testAfterSaveCommitWithTransactionRunning()
+    {
+        $table = TableRegistry::get('users');
+        $data = new \Cake\ORM\Entity([
+            'username' => 'superuser',
+            'created' => new Time('2013-10-10 00:00'),
+            'updated' => new Time('2013-10-10 00:00')
+        ]);
+
+        $called = false;
+        $listener = function ($e, $entity, $options) use (&$called) {
+            $called = true;
+        };
+        $table->eventManager()->attach($listener, 'Model.afterSaveCommit');
+
+        $this->connection->begin();
+        $this->assertSame($data, $table->save($data));
+        $this->assertFalse($called);
+        $this->connection->commit();
+    }
+
+    /**
      * Asserts that afterSave callback not is called on unsuccessful save
      *
      * @group save