Red
Red

Reputation: 6378

Codeigniter transaction

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.

Upvotes: 4

Views: 7072

Answers (2)

zhudekui
zhudekui

Reputation: 11

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.

Upvotes: 1

Red
Red

Reputation: 6378

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();    
}

Upvotes: 1

Related Questions