php - Is it possible to rollback function action in CodeIgniter -
suppose wan't insert data 2 different tables, in codeigniter,
for example :
insert userprofile table: name , mail (get id , use in next table)
insert employees table : userprofile_id, shifts , position.
up now, working good, can if want commit action if both insertions commited...how can rollback in case first insertion went , don't want empty profile.... problem once first insertion committed ,suppose went wrong in next insertion - data 1 table... ideas?
see transactions in codeigniter.
syntax(given in document well):
$this->db->trans_begin(); $this->db->query('an sql query...'); $this->db->query('another query...'); $this->db->query('and yet query...'); if ($this->db->trans_status() === false) { $this->db->trans_rollback(); } else { $this->db->trans_commit(); }
make sure use $this->db->trans_begin() when running manual transactions, not $this->db->trans_start().
also see strict-mode.
updated
for multiple models can approach(my opinion), not sure if only/best one:
// model_x public function functionx($data) { return $this->db->insert('table_x',$data); } // model_y public function functiony($data) { return $this->db->insert('table_y',$data); } //controller $this->db->trans_start(); $this->model_x->functionx(); $this->model_y->functiony(); $this->db->trans_complete(); if ($this->db->trans_status() === false) { //log error }
Comments
Post a Comment