Skip to content

Commit dbee94b

Browse files
committed
GH-51 Model::transaction() now returns true if committed or false if rolled back
1 parent eb628f1 commit dbee94b

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

lib/Model.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ private function invoke_callback($method_name, $must_exist=true)
16321632
* </code>
16331633
*
16341634
* @param Closure $closure The closure to execute. To cause a rollback have your closure return false or throw an exception.
1635+
* @return boolean True if the transaction was committed, False if rolled back.
16351636
*/
16361637
public static function transaction($closure)
16371638
{
@@ -1642,7 +1643,10 @@ public static function transaction($closure)
16421643
$connection->transaction();
16431644

16441645
if ($closure() === false)
1646+
{
16451647
$connection->rollback();
1648+
return false;
1649+
}
16461650
else
16471651
$connection->commit();
16481652
}
@@ -1651,6 +1655,7 @@ public static function transaction($closure)
16511655
$connection->rollback();
16521656
throw $e;
16531657
}
1658+
return true;
16541659
}
16551660
};
16561661
?>

test/ActiveRecordTest.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,30 +288,33 @@ public function test_cast_defaults()
288288
public function test_transaction_committed()
289289
{
290290
$original = Author::count();
291-
Author::transaction(function() { Author::create(array("name" => "blah")); });
291+
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); });
292292
$this->assert_equals($original+1,Author::count());
293+
$this->assert_true($ret);
293294
}
294-
295+
295296
public function test_transaction_committed_when_returning_true()
296297
{
297298
$original = Author::count();
298-
Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
299+
$ret = Author::transaction(function() { Author::create(array("name" => "blah")); return true; });
299300
$this->assert_equals($original+1,Author::count());
301+
$this->assert_true($ret);
300302
}
301-
303+
302304
public function test_transaction_rolledback_by_returning_false()
303305
{
304306
$original = Author::count();
305-
306-
Author::transaction(function()
307+
308+
$ret = Author::transaction(function()
307309
{
308310
Author::create(array("name" => "blah"));
309311
return false;
310312
});
311-
313+
312314
$this->assert_equals($original,Author::count());
315+
$this->assert_false($ret);
313316
}
314-
317+
315318
public function test_transaction_rolledback_by_throwing_exception()
316319
{
317320
$original = Author::count();
@@ -461,4 +464,4 @@ public function test_get_real_attribute_name()
461464
$this->assert_equals(null, $venue->get_real_attribute_name('invalid_field'));
462465
}
463466
};
464-
?>
467+
?>

0 commit comments

Comments
 (0)