Zend 2 db transactions?

删除回忆录丶 提交于 2019-12-20 23:20:39

问题


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.


回答1:


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



回答2:


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();


来源:https://stackoverflow.com/questions/14228003/zend-2-db-transactions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!