Bluz\Db\Db::transaction PHP Method

transaction() public method

Example of usage $db->transaction(function() use ($db) { $db->query("INSERT INTO table ..."); $db->query("UPDATE table ..."); $db->query("DELETE FROM table ..."); })
public transaction ( callable $process ) : boolean
$process callable callable structure - closure function or class with __invoke() method
return boolean
    public function transaction($process)
    {
        if (!is_callable($process)) {
            throw new DbException('First argument of transaction method should be callable');
        }
        try {
            $this->handler()->beginTransaction();
            call_user_func($process);
            $this->handler()->commit();
            return true;
        } catch (\PDOException $e) {
            $this->handler()->rollBack();
            return false;
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Transaction fail
  * @expectedException \Bluz\Db\Exception\DbException
  */
 public function testTransactionInvalidCallbackThrowException()
 {
     $this->db->transaction('foo');
 }