【发布时间】:2013-05-03 04:56:14
【问题描述】:
我有 2 个模型:modelA、modelB。我想在 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() 失败时它必须回滚。怎么修?非常感谢
【问题讨论】:
-
您也可以发布您的型号代码吗?
-
只要您的模型方法中没有其他事务语句,您的示例代码应该可以正常工作。
标签: php mysql codeigniter transactions