Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend 2 db transactions?

How do we use transactions in Zend 2? I didn't find anything in the API, and a couple questions for Zend 1 refered to the regular PDO functions, but I don't see anything like that in Zend 2.

like image 880
Raekye Avatar asked Jan 09 '13 03:01

Raekye


2 Answers

The documentation is lacking a bit in this department for ZF2:

Start Transaction:

$this->adapter->getDriver()->getConnection()->beginTransaction();

Commit Transaction:

$this->adapter->getDriver()->getConnection()->commit();

Rollback Transaction:

$this->adapter->getDriver()->getConnection()->rollback();
like image 126
Diemuzi Avatar answered Oct 17 '22 02:10

Diemuzi


Try this:

$adapter = new Zend\Db\Adapter\Adapter(array(
    'driver' => 'pdo',
    'dsn' => 'mysql:dbname=db;hostname=localhost',
    'username' => 'root',
    'password' => 'password',
    'driver_options' => array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
    ),
));

$adapter->getDriver()->getConnection()->beginTransaction();

DB will run command:

START TRANSACTION
like image 7
AlloVince Avatar answered Oct 17 '22 02:10

AlloVince