\RedBeanPHP\UtilTransaction

Transaction Helper

This code was originally part of the facade, however it has been decided to remove unique features to service classes like this to make them available to developers not using the facade class.

Database transaction helper. This is a convenience class to perform a callback in a database transaction. This class contains a method to wrap your callback in a transaction.

Summary

Methods
Properties
Constants
transaction()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

transaction()

transaction(\RedBeanPHP\Adapter  $adapter, callable  $callback) : mixed

Wraps a transaction around a closure or string callback.

If an Exception is thrown inside, the operation is automatically rolled back. If no Exception happens, it commits automatically. It also supports (simulated) nested transactions (that is useful when you have many methods that needs transactions but are unaware of each other).

Example:

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

R::transaction(function() use($from, $to, $amount) { $accountFrom = R::load('account', $from); $accountTo = R::load('account', $to); $accountFrom->money -= $amount; $accountTo->money += $amount; R::store($accountFrom); R::store($accountTo); });

Parameters

\RedBeanPHP\Adapter $adapter

Database Adapter providing transaction mechanisms.

callable $callback

Closure (or other callable) with the transaction logic

Returns

mixed