php - Issue about transaction in CodeIgniter -
i have 2 models: modela, modelb. want implement 2 operations related these models in ci's transaction in mycontroller. e.x:
$this->db->trans_start(); $this->modela->uodatea(conditions, item); $this->modelb->updateb(conditions, item); // conditions , item valid $this->db->trans_complete(); if ($this->db->trans_status() === false) { //handle when failed }
here 2 operation in these models:
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); }
although updateb() failed (ex: can't have record update, update int field string value...) trans_status() still return true. want when updateb() failed must rollback. how fix? much
you can try one
$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(); }
Comments
Post a Comment