gabordemooij/redbean — pull request #245 by hugollm (2013-02-28 02:44:14+00:00)
Title: RedBean_Facade::transaction

A method to help removing transaction overhead in code. It automatically commits or rolls back (if an Exception happens).

$from = 1;
$to = 2;
$ammount = 300;

R::transaction(function() use($from, $to, $ammount)
{
$accountFrom = R::load('account', $from);
$accountTo = R::load('account', $to);

$accountFrom->money -= $ammount;
$accountTo->money += $ammount;

R::store($accountFrom);
R::store($accountTo);
});

It also support (simulated) nested transactions. Useful when you have multiple methods that needs transactions but are unaware of each other.

function foo()
{
R::transaction(function()
{
// do something...
});
}

function bar()
{
R::transaction(function()
{
// do something...
});
}

function biz()
{
R::transaction(function()
{
foo();
bar();
});
}