Database transaction not working in codeigniter controller

I am calling multiple models in controllers and all models are making a database query. i did something like this

public function InsertSale()
    {
            $this->db->trans_start(TRUE);
            // all logic part and models calling which do insert/update/delete

            $this->db->trans_complete();
}

      

The above code doesn't work even if something doesn't work after some requests they don't roll back.

+3


source to share


2 answers


If TRUE

to $this->db->trans_start(TRUE);

translate the transaction in the test mode, which means that no matter what happens, the request will be rolled back.

If you want to see if a query will work, you should use:

$this->db->trans_status();

      



which will return either TRUE / FALSE depending on the result.

(I realize this is an old question, however, I thought I should answer this question in case someone else looked.)

Hope this helps!

0


source


So, you need to follow like this

$this->db->trans_start(); # Starting Transaction
$this->db->trans_strict(FALSE); 

$this->db->insert('table_name', $someDataArray); # Inserting data

# Updating data
$this->db->where('id', $id);
$this->db->update('table_name', $someDataArray); 

$this->db->trans_complete();

      



It works well. Check also this answer

0


source







All Articles