Unit of Work

From RedBean

Jump to: navigation, search

Unit Of Work in RedBeanPHP

RedBean supports a unique implementation of the Unit Of Work pattern (closure based unit of work or CUOW). To add some work:

$uow->addWork("save", function() use ($myModel) {  $myModel->save(); }); 
$uow->addWork("save", function() use ($redbean, $aBean) {  $redbean->store( $aBean ); });
$uow->addWork("delete", function() { ... } );

You can tag each kind of work. To do the work:

$uow->doWork("save");

If you want to use transactions with the unit of work pattern, simply copy/paste this code:

$uow = new RedBean_UnitOfWork;
$uow->addWork("saveAll", function() use($uow, $adapter){
 $adapter->startTransaction(); 
 $uow->doWork("save");
 $adapter->commit(); 
});

From now on, you can use saveAll to save all models or beans within a single transaction. You can do the same for delete.


This feature requires PHP 5.3 or higher.


API Class Reference: RedBean_UnitOfWork