如何在Zend_Db_Table中使用事务?

时间:2013-01-14 03:01:25

标签: zend-framework transactions

由于历史原因,所有模型都从Zend_Db_Table扩展。而现在,我需要使用交易。如何?

我做了一些谷歌搜索,一些人说以下方式可以帮助:

1:$ tableA-> getAdapter() - > beginTransaction(); 2:$ tableA-> getAdapter() - > getDriver() - > getConnection() - > beginTransaction();

有没有更好的解决方案?

1 个答案:

答案 0 :(得分:-2)

我通过在zend_db_table中添加以下代码解决了这个问题: / **      *支持交易      * /

public function beginTransaction() {
    $this->getAdapter()->beginTransaction();
}
public function commit() {
    $this->getAdapter()->commit();
}
public function rollback() {
    $this->getAdapter()->rollback();
}

这样我就可以在从zend_db_table扩展的模型中使用事务,如下所示:

public function test(){
    $this->beginTransaction();

    try {
        $this->addCourseItem('1', '1', '1', '1', '1');
        $this->fetchRow("none-exist-field = 1");
        //the following code will not execute
        echo "okay";
        $this->commit();
    } catch (exception $e) {
        $this->rollback();
        echo "error message:".$e->getMessage();
    }

}