我有2个模型:modelA,modelB。我想在myController的CI事务中实现与这些模型相关的2个操作。例如:关于CodeIgniter中事务的问题
$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()失败时它必须回滚。怎么修?非常感谢
你可以发布你的模型代码吗? – 2013-05-03 05:03:58
@Kishor:我更新了 – secretlm 2013-05-03 07:38:44
只要您的模型方法中没有其他交易语句,您的示例代码应该可以正常工作。 – 2013-05-03 09:43:01