Permalink
Browse files

GH-51 Model::transaction() now returns true if committed or false if …

…rolled back
  • Loading branch information...
kla committed May 19, 2010
1 parent eb628f1 commit dbee94b9efd9045422fbab3cbaeab785ca355725
Showing with 17 additions and 9 deletions.
  1. +5 −0 lib/Model.php
  2. +12 −9 test/ActiveRecordTest.php
View
@@ -1632,6 +1632,7 @@ private function invoke_callback($method_name, $must_exist=true)
* </code>
*
* @param Closure $closure The closure to execute. To cause a rollback have your closure return false or throw an exception.
* @return boolean True if the transaction was committed, False if rolled back.
*/
public static function transaction($closure)
{
@@ -1642,7 +1643,10 @@ public static function transaction($closure)
$connection->transaction();
if ($closure() === false)
{
$connection->rollback();
return false;
}
else
$connection->commit();
}
@@ -1651,6 +1655,7 @@ public static function transaction($closure)
$connection->rollback();
throw $e;
}
return true;
}
};
?>
View
@@ -288,30 +288,33 @@ public function test_cast_defaults()
public function test_transaction_committed()
{
$original = Author::count();
Author::transaction(function() { Author::create(array("name" => "blah")); });
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); });
$this->assert_equals($original+1,Author::count());
$this->assert_true($ret);
}
public function test_transaction_committed_when_returning_true()
{
$original = Author::count();
Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
$this->assert_equals($original+1,Author::count());
$this->assert_true($ret);
}
public function test_transaction_rolledback_by_returning_false()
{
$original = Author::count();
Author::transaction(function()
$ret = Author::transaction(function()
{
Author::create(array("name" => "blah"));
return false;
});
$this->assert_equals($original,Author::count());
$this->assert_false($ret);
}
public function test_transaction_rolledback_by_throwing_exception()
{
$original = Author::count();
@@ -461,4 +464,4 @@ public function test_get_real_attribute_name()
$this->assert_equals(null, $venue->get_real_attribute_name('invalid_field'));
}
};
?>
?>

0 comments on commit dbee94b

Please sign in to comment.