Welcome Guest, Not a member yet? Register   Sign In
Codeigniter transactions with multiple functions
#6

(06-21-2017, 09:04 AM)skunkbad Wrote: Yes. Think about code execution. Just because code execution goes in and out of functions doesn't mean that the DB class isn't going to handle what it's told to do. Functions/methods do limit variable scope, help organize your code, and have other benefits. Functions/methods aren't going to interfere with your transactions.

I have performed the tests but if an insert fails all queries are executed and does not undo the queries that have to be undone in order to maintain the integrity of the data.

PHP Code:
<?php

public function init_invoice()
{
 
   $products self::products_related_invoice();
 
   $total '0';

 
   if(!$products) {
 
       return;
 
   }

 
   foreach ($products as $value) {
 
       $total $total $value['price'];
 
   }

 
   $this->db->trans_start();
 
   $id_invoice             self::add_new_invoice($total);
 
   $add_invoice_details    self::add_invoice_details($id_invoice$products);

 
   if ($this->db->trans_status() === FALSE) {
 
       $this->db->trans_rollback();
 
       return false;
 
   } else {
 
       $this->db->trans_commit();
 
       return true;
 
   }
}

public function 
add_new_invoice($total)
{
 
  $data = array(            
        
'id_user'           => $this->session->user_id,
 
       'id_invoice_status' => '1'
 
       'total' => $total
 
       'auto_create' => 
    
);
 
   $this->db->insert('bill'$data);
 
   if($this->db->affected_rows()) {
 
       $id_invoice $this->db->insert_id();       
        return $id_invoice
;
 
   }
 
   return false;
}

public function 
add_invoice_details($id_invoice$productos)
{
 
   /**
     * Default var
     */
 
   $status false;

 
   foreach ($productos as $key => $value) {
 
       $query $this->db->simple_query('
            INSERT INTO '
.$this->db->dbprefix('bill_details').'
                   (`id_invoice`,`id_product`,`created_by`,`modified_by`) 
            VALUES (
                '
.$this->db->escape($id_invoice).',
                '
.$this->db->escape($value['id']).' ,
                '
.$this->db->escape($this->session->user_id).'
                '
.$this->db->escape($this->session->user_id).'
            )
        '
);

 
       if(!$query) {
 
           $status true;
 
       }
 
   }

 
   if($status == false) {
 
       return true;
 
   }

 
   return false;
}

public function 
products_related_invoice()
{
 
   $query $this->db->select('id, price')
 
                     ->from('products')
 
                     ->where('status''1'
 
                     ->where('related_invoice''1'
 
                     ->get();
 
   if($query->num_rows() > 0) {
 
       
        $data 
$query->result_array();           
        return $data
;
 
   }
 
   return false;

Reply


Messages In This Thread
RE: Codeigniter transactions with multiple functions - by funcion - 06-21-2017, 11:26 AM



Theme © iAndrew 2016 - Forum software by © MyBB