0

我有2个模型:模型A,模型B。我想在 myController 的 CI 事务中实现 2 个与这些模型相关的操作。前任:

$this->db->trans_start();

$this->modelA->uodateA(conditions, item);
$this->modelB->updateB(conditions, item);
// with conditions and item is valid
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
//handle when failed
}

以下是这些模型中的 2 个操作:

public function updateA($where = array(), $item = array(), $auth = NULL){
        if(!is_array($item) && empty($item)) return FALSE;

        if(is_numeric($where)){
            $where = array('vote_id'=>$where);
        }
        $this->db->where($where)->update($this->_table,$item);


        return    ($this->db->affected_rows() > 0);
    }


public function updateB($where = array(), $item = array(), $auth = NULL){
        if(!is_array($item) && empty($item)) return FALSE;

            if(is_numeric($where)){
                $where = array('vote_id'=>$where);
            }

        $this->db->where($where)->update($this->_table,$item);
        return ($this->db->affected_rows() > 0);
    }

虽然 updateB() 失败(例如:不能更新记录,用字符串值更新 int 字段...)但 trans_status() 仍然返回 true。我想当 updateB() 失败时它必须回滚。怎么修?非常感谢

4

1 回答 1

0

你可以试试这个

 $this->db->trans_begin();

 $this->modelA->insertA();
 $this->modelB->updateB();

 if ($this->db->trans_status() === FALSE)
 {
    $this->db->trans_rollback();
 }
 else
 {
    $this->db->trans_commit();
 }
于 2013-05-03T06:25:10.817 回答