代码改变世界

Can we lock tables while executing a query in Zend-Db

2013-02-19 14:41  游乐场123  阅读(214)  评论(0编辑  收藏  举报
$db->beginTransaction();try{
    $select = $db->select()->forUpdate()// <-- here's the magic->from(
                     array('a'=>'yourTable'),
                     array('your','column','names'))->where('someColumn = ?', $whatever );

    $result = $this->_adapter->fetchRow( $select );/*
      alter data in $result
      and update if necessary:
    */
    $db->update('yourTable', $result, array('someColumn = ?'=> $whatever ));

    $db->commit();}catch(Exception $e ){
    $db->rollBack();}



// Start a transaction explicitly.
$db->beginTransaction();try{//Get your data $sql ="SELECT * FROM page WHERE col='value'"; $result = $db->fetchAll($sql);//Make the insert $data = array('col1'=>'val1','col2'=>'val2'); $db->insert('page', $data);// If all succeed, commit the transaction and all changes// are committed at once. $db->commit();}catch(Exception $e){// If any of the queries failed and threw an exception,// we want to roll back the whole transaction, reversing// changes made in the transaction, even those that succeeded.// Thus all changes are committed together, or none are. $db->rollBack(); echo $e->getMessage();}