我有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