3 namespace RedBeanPHP\Util;
5 use RedBeanPHP\OODB as OODB;
6 use RedBeanPHP\OODBBean as OODBBean;
7 use RedBeanPHP\RedException as RedException;
8 use RedBeanPHP\Adapter as Adapter;
13 * This code was originally part of the facade, however it has
14 * been decided to remove unique features to service classes like
15 * this to make them available to developers not using the facade class.
17 * Database transaction helper. This is a convenience class
18 * to perform a callback in a database transaction. This class
19 * contains a method to wrap your callback in a transaction.
21 * @file RedBeanPHP/Util/Transaction.php
22 * @author Gabor de Mooij and the RedBeanPHP Community
26 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community
27 * This source file is subject to the BSD/GPLv2 License that is bundled
28 * with this source code in the file license.txt.
33 * Wraps a transaction around a closure or string callback.
34 * If an Exception is thrown inside, the operation is automatically rolled back.
35 * If no Exception happens, it commits automatically.
36 * It also supports (simulated) nested transactions (that is useful when
37 * you have many methods that needs transactions but are unaware of
47 * R::transaction(function() use($from, $to, $amount)
49 * $accountFrom = R::load('account', $from);
50 * $accountTo = R::load('account', $to);
51 * $accountFrom->money -= $amount;
52 * $accountTo->money += $amount;
53 * R::store($accountFrom);
54 * R::store($accountTo);
58 * @param Adapter $adapter Database Adapter providing transaction mechanisms.
59 * @param callable $callback Closure (or other callable) with the transaction logic
63 public static function transaction( Adapter $adapter, $callback )
65 if ( !is_callable( $callback ) ) {
66 throw new RedException( 'R::transaction needs a valid callback.' );
73 $adapter->startTransaction();
76 $result = call_user_func( $callback ); //maintain 5.2 compatibility
81 } catch ( \Exception $exception ) {