transaction can't rollback in codeigniter transaction can't rollback in codeigniter codeigniter codeigniter

transaction can't rollback in codeigniter


Do not use $this->db->trans_complete();

as per documentation

$this->db->trans_begin();$this->db->query('AN SQL QUERY...');$this->db->query('ANOTHER QUERY...');$this->db->query('AND YET ANOTHER QUERY...');if ($this->db->trans_status() === FALSE){        $this->db->trans_rollback();}else{        $this->db->trans_commit();}


ANSWER UPDATED!

I have to tell you the default of codeigniter´s config is automatically commit all transactions.

If for any reason you want to disable this function you have to use this line:

$this->db->trans_off();    

before of

$this->db->begin();

Said that, so when you use

$this->db->trans_complete();

you will need to commit or rollback, like this:

$result = $this->db->trans_complete();if($result === true){    $this->db->trans_commit();}else{    $this->db->trans_rollback();}

Finally what you want is:

$this->db->trans_off();$this->db->query("insert this...");$this->db->query("insert that...");$result = $this->db->trans_complete();if($result === true){    $this->db->trans_commit();}else{    $this->db->trans_rollback();}


As I know for CI version 3.0.1 the only thing that you should worry about is where to put $this->db->trans_start(); and $this->db->trans_complete();

So for situations like:

$this->db->trans_start();//any code goes here$this->db->trans_complete();

Transaction will be rolled back if something goes wrong, or will be committed on call to $this->db->trans_complete();

Because if you look under the hood of trans_start method it contains trans_begin. And trans_complete method contains check for trans_status and accordingly calls to trans_commit or trans_rollback methods.

Same applies for nested transactions:

$this->db->trans_start();//any code goes here$this->db->trans_start();//any code goes here$this->db->trans_complete();//any code goes here$this->db->trans_complete();

By default Codeigniter runs all transactions in Strict Mode so on last trans_complete all will be committed, or if anything fails all rollbacks.