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