transaction() public static method

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); });
public static transaction ( callable $callback ) : mixed
$callback callable Closure (or other callable) with the transaction logic
return mixed
Esempio n. 1
0
 /**
  * Test Facade transactions.
  * 
  * @return void
  * 
  * @throws\Exception
  */
 public function testTransactionInFacade()
 {
     testpack('Test transaction in facade');
     $bean = R::dispense('bean');
     $bean->name = 'a';
     R::store($bean);
     R::trash($bean);
     R::freeze(TRUE);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     R::store($bean);
     asrt(R::count('bean'), 1);
     R::trash($bean);
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     $id = R::transaction(function () use(&$bean) {
         return R::transaction(function () use(&$bean) {
             return R::store($bean);
         });
     });
     asrt((int) $id, (int) $bean->id);
     R::trash($bean);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     $id = R::transaction(function () use(&$bean) {
         return R::store($bean);
     });
     asrt((int) $id, (int) $bean->id);
     R::trash($bean);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::store($bean);
             R::transaction(function () {
                 throw new \Exception();
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::transaction(function () use($bean) {
                 R::store($bean);
                 throw new \Exception();
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 0);
     $bean = R::dispense('bean');
     $bean->name = 'a';
     try {
         R::transaction(function () use($bean) {
             R::transaction(function () use($bean) {
                 R::store($bean);
             });
         });
     } catch (\Exception $e) {
         pass();
     }
     asrt(R::count('bean'), 1);
     R::freeze(FALSE);
     try {
         R::transaction('nope');
         fail();
     } catch (\Exception $e) {
         pass();
     }
     testpack('Test Camelcase 2 underscore');
     $names = array('oneACLRoute' => 'one_acl_route', 'ALLUPPERCASE' => 'alluppercase', 'clientServerArchitecture' => 'client_server_architecture', 'camelCase' => 'camel_case', 'peer2peer' => 'peer2peer', 'fromUs4You' => 'from_us4_you', 'lowercase' => 'lowercase', 'a1A2b' => 'a1a2b');
     $bean = R::dispense('bean');
     foreach ($names as $name => $becomes) {
         $bean->{$name} = 1;
         asrt(isset($bean->{$becomes}), TRUE);
     }
     testpack('Misc Tests');
     R::debug(1);
     flush();
     ob_start();
     R::exec('SELECT 123');
     $out = ob_get_contents();
     ob_end_clean();
     flush();
     pass();
     asrt(strpos($out, 'SELECT 123') !== FALSE, TRUE);
     R::debug(0);
     flush();
     ob_start();
     R::exec('SELECT 123');
     $out = ob_get_contents();
     ob_end_clean();
     flush();
     pass();
     asrt($out, '');
     R::debug(0);
     pass();
     testpack('test to string override');
     $band = R::dispense('band');
     $str = strval($band);
     asrt($str, 'bigband');
     testpack('test whether we can use isset/set in model');
     $band->setProperty('property1', 123);
     asrt($band->property1, 123);
     asrt($band->checkProperty('property1'), TRUE);
     asrt($band->checkProperty('property2'), FALSE);
     $band = new \Model_Band();
     $bean = R::dispense('band');
     $bean->property3 = 123;
     $band->loadBean($bean);
     $bean->property4 = 345;
     $band->setProperty('property1', 123);
     asrt($band->property1, 123);
     asrt($band->checkProperty('property1'), TRUE);
     asrt($band->checkProperty('property2'), FALSE);
     asrt($band->property3, 123);
     asrt($band->property4, 345);
     testpack('Can we pass a\\PDO object to Setup?');
     $pdo = new \PDO('sqlite:test.db');
     R::addDatabase('pdo', $pdo);
     R::selectDatabase('pdo');
     R::getCell('SELECT 123;');
     testpack('Test array interface of beans');
     $bean = R::dispense('bean');
     $bean->hello = 'hi';
     $bean->world = 'planet';
     asrt($bean['hello'], 'hi');
     asrt(isset($bean['hello']), TRUE);
     asrt(isset($bean['bye']), FALSE);
     $bean['world'] = 'sphere';
     asrt($bean->world, 'sphere');
     foreach ($bean as $key => $el) {
         if ($el == 'sphere' || $el == 'hi' || $el == 0) {
             pass();
         } else {
             fail();
         }
         if ($key == 'hello' || $key == 'world' || $key == 'id') {
             pass();
         } else {
             fail();
         }
     }
     asrt(count($bean), 3);
     unset($bean['hello']);
     asrt(count($bean), 2);
     asrt(count(R::dispense('countable')), 1);
     // Otherwise untestable...
     $bean->setBeanHelper(new SimpleFacadeBeanHelper());
     R::getRedBean()->setBeanHelper(new SimpleFacadeBeanHelper());
     pass();
     // Test whether properties like owner and shareditem are still possible
     testpack('Test Bean Interface for Lists');
     $bean = R::dispense('bean');
     // Must not be list, because first char after own is lowercase
     asrt(is_array($bean->owner), FALSE);
     // Must not be list, because first char after shared is lowercase
     asrt(is_array($bean->shareditem), FALSE);
     asrt(is_array($bean->own), FALSE);
     asrt(is_array($bean->shared), FALSE);
     asrt(is_array($bean->own_item), FALSE);
     asrt(is_array($bean->shared_item), FALSE);
     asrt(is_array($bean->{'own item'}), FALSE);
     asrt(is_array($bean->{'shared Item'}), FALSE);
 }