4

How transaction in codeIgniter works? can I stop once transaction and start one another?

See my example

$this->db->trans_begin(); $a = 'UPDATE ......'; RETURN TRUE $b = 'INSERT INTO......'; RETURN FALSE $this->db->trans_rollback(); // I tried $this->db->trans_off(); var_dump( $this->db->trans_status() ); $this->db->trans_begin(); if ( $this->db->trans_status() === FALSE ) { $this->db->trans_rollback(); } else { $this->db->trans_commit(); } 

My first transaction will always return FALSE (even it is true I need to rollback it) ,now I need to close that transaction and need to start another one.

The problem is with $this->db->trans_status() , it always returns FALSE on the second transaction (even after $this->db->trans_rollback() or trans_off()).

what am I doing wrong ? Please help me.

I am using mySql as underlying database. 
12
  • 1
    In var_dump you're using trans_status rather than trans_status() Commented Feb 12, 2013 at 4:59
  • @ExplosionPills that was a typo. Commented Feb 12, 2013 at 5:03
  • 1
    are you using innodb or not? Commented Feb 12, 2013 at 5:04
  • @mamdouhalramadan YES. Commented Feb 12, 2013 at 5:15
  • 1
    Ok, so is your error reporting enabled in config/database.php and if yes what is the error you are getting? Commented Feb 12, 2013 at 5:24

2 Answers 2

1

It works OK now with manually setting the trans_status

$this->db->trans_begin(); $a = 'UPDATE ......'; RETURN TRUE $b = 'INSERT INTO......'; RETURN FALSE $this->db->trans_rollback(); //First transaction ends it will return FALSE always(in my case) $this->db->_trans_status = TRUE; // setting the trans_status manually ,so it will ignore previous attempts $this->db->trans_begin(); //other operations .. if ( $this->db->trans_status() === FALSE ) { $this->db->trans_rollback(); } else { $this->db->trans_commit(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

$this->db->trans_status() = TRUE; doesn't set anything! trans_status() is a function/method. You cannot assign value in return context.
@machineaddict that was a typo, fixed.
1

I think you need add this before transaction start:

$this->db->trans_strict(FALSE); 

By default CodeIgniter runs all transactions in Strict Mode.

When strict mode is enabled, if you are running multiple groups of transactions, if one group fails all groups will be rolled back.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.