I'm using Codeigniter transactions
$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->trans_complete();
This works fine , the problem I have is that inside the trans_start
and trans_complete
I'm calling other functions and those functions deals with database so they contains inserts and update and some deletes ... ex:
$this->db->trans_start();
$this->utils->insert_function($data);
$this->utils->update_function2($test);
$this->db->trans_complete();
Now if those functions are executed and some errors occur CodeIgniter won't do a rollback.
What is the best way to deal with such issue?
The only solution, I have in mind, is to return an error from those functions and inside those function add (trans_stat
and trans_complete
) And if it returns an error test an do $this->db->trans_rollback
ex:
$this->db->trans_start();
$result = $this->utils->insert_function($data);
if($result === false){
$this->db->trans_rollback();
}
$this->db->trans_complete();
Is there a better way of doing this?
Update 1:
As requested a sample of the external function i'm calling :
// insert_function contains
$rec = array(
'numero' => $numero,
'transaction_id' => $id,
'debit' => $product_taxes['amount_without_taxes'],
'date' => $data['date_transaction'],
);
$this->addExerciceAccountingRecords($rec);
and addExerciceAccountingRecords contains
function addExerciceAccountingRecords($records) {
$this->db->insert('transactions_exercices', $records);
}