[limb-svn] r5868 - in 3.x/trunk/limb/active_record: src tests/cases

svn at limb-project.com svn at limb-project.com
Fri May 11 18:52:12 MSD 2007


Author: pachanga
Date: 2007-05-11 18:52:12 +0400 (Fri, 11 May 2007)
New Revision: 5868
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5868

Added:
   3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTransactionTest.class.php
Modified:
   3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
Log:
-- lmbActiveRecord :: _doSave() rolls back transaction if there were any exception 
-- lmbActiveRecord :: trySave() catches generic Exception not just lmbValidationException

Modified: 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php
===================================================================
--- 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-05-11 14:13:55 UTC (rev 5867)
+++ 3.x/trunk/limb/active_record/src/lmbActiveRecord.class.php	2007-05-11 14:52:12 UTC (rev 5868)
@@ -722,70 +722,79 @@
     if($this->_is_being_saved)
       return;
 
-    $this->_is_being_saved = true;
+    try
+    {
+      $this->_is_being_saved = true;
 
-    $this->_savePreRelations();
+      $this->_savePreRelations();
 
-    $this->_onBeforeSave();
+      $this->_onBeforeSave();
 
-    $this->_invokeListeners(self :: ON_BEFORE_SAVE);
+      $this->_invokeListeners(self :: ON_BEFORE_SAVE);
 
-    if(!$this->isNew() && $this->isDirty())
-    {
-      $this->_onBeforeUpdate();
+      if(!$this->isNew() && $this->isDirty())
+      {
+        $this->_onBeforeUpdate();
 
-      $this->_invokeListeners(self :: ON_BEFORE_UPDATE);
+        $this->_invokeListeners(self :: ON_BEFORE_UPDATE);
 
-      if($need_validation && !$this->_validateUpdate())
-        throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
-                                         $this->_error_list);
+        if($need_validation && !$this->_validateUpdate())
+          throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
+                                           $this->_error_list);
 
-      $this->_onSave();
+        $this->_onSave();
 
-      $this->_onUpdate();
+        $this->_onUpdate();
 
-      $this->_invokeListeners(self :: ON_UPDATE);
+        $this->_invokeListeners(self :: ON_UPDATE);
 
-      $this->_updateDbRecord($this->_propertiesToDbFields());
+        $this->_updateDbRecord($this->_propertiesToDbFields());
 
-      $this->_onAfterUpdate();
+        $this->_onAfterUpdate();
 
-      $this->_invokeListeners(self :: ON_AFTER_UPDATE);
-    }
-    elseif($this->isNew())
-    {
-      $this->_onBeforeCreate();
+        $this->_invokeListeners(self :: ON_AFTER_UPDATE);
+      }
+      elseif($this->isNew())
+      {
+        $this->_onBeforeCreate();
 
-      $this->_invokeListeners(self :: ON_BEFORE_CREATE);
+        $this->_invokeListeners(self :: ON_BEFORE_CREATE);
 
-      if($need_validation && !$this->_validateInsert())
-        throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
-                                         $this->_error_list);
+        if($need_validation && !$this->_validateInsert())
+          throw new lmbValidationException('ActiveRecord "' . get_class($this) . '" validation failed',
+                                           $this->_error_list);
 
-      $this->_onSave();
+        $this->_onSave();
 
-      $this->_onCreate();
+        $this->_onCreate();
 
-      $this->_invokeListeners(self :: ON_CREATE);
+        $this->_invokeListeners(self :: ON_CREATE);
 
-      $new_id = $this->_insertDbRecord($this->_propertiesToDbFields());
-      $this->_is_new = false;
-      $this->setId($new_id);
+        $new_id = $this->_insertDbRecord($this->_propertiesToDbFields());
+        $this->_is_new = false;
+        $this->setId($new_id);
 
-      $this->_onAfterCreate();
+        $this->_onAfterCreate();
 
-      $this->_invokeListeners(self :: ON_AFTER_CREATE);
-    }
+        $this->_invokeListeners(self :: ON_AFTER_CREATE);
+      }
 
-    $this->_onAfterSave();
+      $this->_onAfterSave();
 
-    $this->_invokeListeners(self :: ON_AFTER_SAVE);
+      $this->_invokeListeners(self :: ON_AFTER_SAVE);
 
-    $this->_savePostRelations();
+      $this->_savePostRelations();
 
-    $this->_resetDirty();
+      $this->_resetDirty();
 
-    $this->_is_being_saved = false;
+      $this->_is_being_saved = false;
+    }
+    catch(Exception $e)
+    {
+      $conn = lmbToolkit :: instance()->getDefaultDbConnection();
+      $conn->rollbackTransaction();
+      throw $e;
+    }
 
     return $this->getId();
   }
@@ -858,7 +867,7 @@
     {
       $this->save($error_list);
     }
-    catch(lmbValidationException $e)
+    catch(Exception $e)
     {
       return false;
     }

Added: 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTransactionTest.class.php
===================================================================
--- 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTransactionTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/active_record/tests/cases/lmbActiveRecordTransactionTest.class.php	2007-05-11 14:52:12 UTC (rev 5868)
@@ -0,0 +1,76 @@
+<?php
+/**
+ * Limb Web Application Framework
+ *
+ * @link http://limb-project.com
+ *
+ * @copyright  Copyright &copy; 2004-2007 BIT
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html
+ * @version    $Id: lmbActiveRecordTest.class.php 5863 2007-05-11 12:56:42Z pachanga $
+ * @package    active_record
+ */
+lmb_require('limb/active_record/src/lmbActiveRecord.class.php');
+lmb_require('limb/dbal/src/lmbSimpleDb.class.php');
+
+class TestOneTableObjectFailing extends lmbActiveRecord
+{
+  var $fail = false;
+  protected $_db_table_name = 'test_one_table_object';
+
+  protected function _onAfterSave()
+  {
+    if($this->fail)
+      throw new Exception('catch me');
+  }
+}
+
+class lmbActiveRecordTransactionTest extends UnitTestCase
+{
+  function setUp()
+  {
+    $this->conn = lmbToolkit :: instance()->getDefaultDbConnection();
+    $this->db = new lmbSimpleDb($this->conn);
+
+    $this->_cleanUp();
+  }
+
+  function tearDown()
+  {
+    $this->_cleanUp();
+  }
+
+  function _cleanUp()
+  {
+    $this->db->delete('test_one_table_object');
+  }
+
+  function  testSaveInTransaction()
+  {
+    $this->conn->beginTransaction();
+
+    $obj = new TestOneTableObjectFailing();
+    $obj->setContent('hey');
+
+    $this->assertTrue($obj->trySave());
+
+    $this->conn->commitTransaction();
+
+    $this->assertEqual($this->db->count('test_one_table_object'), 1);
+  }
+
+  function  testSaveRollbacksTransaction()
+  {
+    $this->conn->beginTransaction();
+
+    $obj = new TestOneTableObjectFailing();
+    $obj->setContent('hey');
+    $obj->fail = true;
+
+    $this->assertFalse($obj->trySave());
+
+    $this->conn->commitTransaction();
+
+    $this->assertEqual($this->db->count('test_one_table_object'), 0);
+  }
+}
+?>



More information about the limb-svn mailing list