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.
source to share
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!
source to share
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
source to share