⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

call back after insert produces two inserts on my tables



mlopez1965
  • profile picture
  • Member

Posted 29 July 2015 - 22:25 PM

Hi guys

 

Fantastic tool this GC...

hope you can help me... I´v checked on this forum and nothing alike is here...
I´m trying to post fill some data in my table using call_back_after_insert.

my function is:

 

public function clienteyserie (){
       
        $id_clientes = $this->session->userdata('id_clientes');
        $id_serial_cliente = $this->session->userdata ('id_serial_cliente');
        
        $query = $this->db->get_where('clientes', array('id_clientes' => $id_clientes));
        $query = $query->result_array();
        $id_clientes = $query[0]['empresa_cliente'];
        
        $query = $this->db->get_where('series_cliente', array('id_serial_cliente' => $id_serial_cliente));
        $query = $query->result_array();
        $id_serial_cliente = $query[0]['serie'];
        
        $data = array(
            'cliente'=> $id_clientes,
            'serie' => $id_serial_cliente,
        );
        $this->db->insert('tarealog', $data);     
        
        return true;
    }

 

and my call_back function is:

 $crud->callback_after_insert(array($this,'clienteyserie'));

 

My problem is that on the target table, I´m getting the data in two different rows instead of a single one?

 

   One row for the data table...

   One row for the function data...

 

can please help? it may me something obvious, but I have 5 hours on this, without a clear view...

 

thanks in advanced


Paul Savostin
  • profile picture
  • Member

Posted 29 July 2015 - 23:53 PM

Hi! So actualy I dont understand, what is wrong? one row insert by GC and second by your callback function.


mlopez1965
  • profile picture
  • Member

Posted 30 July 2015 - 00:57 AM

Hi, thanks for your fast answer !

 

According with your answer, this means that is a natural behavior to have those two inserts??

On my mind, I understood that the callback would produce a single insert within the table.

 

please, if so? is there a GC way to have only one insert?

Or, do I have to arrrange that on my codeigniter active record?

 

best regards


Paul Savostin
  • profile picture
  • Member

Posted 30 July 2015 - 09:21 AM

If you need to do smth with insert data and want one insert

1)use before_insert/update

2)you can even ovveride insert/update with your own function

3)and finally in callback_after_insert/update u have access to post array and primary inserted key

So it all depends what u need to achieve :)


mlopez1965
  • profile picture
  • Member

Posted 30 July 2015 - 18:19 PM

Thanks for the feedback, I did fix yesterdary only with Active Record funtions.

here is the code, hoping will be usefull for someone in the future....

 

for the function:

 

 public function clienteyserie (){
        //bajamos los valores de variable serie y cliente
        $id_clientes = $this->session->userdata('id_clientes');
        $id_serial_cliente = $this->session->userdata ('id_serial_cliente');
        
        // obtenemos el lastid de la tabla 'tarea log'
        $lastid = 0;
        $row = $this->db->query("SELECT MAX(id_tarea) AS 'lastid' FROM `tarealog`")-> row();
        if ($row){
            $lastid = $row->lastid;
        }
        
                
        $query = $this->db->get_where('clientes', array('id_clientes' => $id_clientes));
        $query = $query->result_array();
        $id_clientes = $query[0]['empresa_cliente'];
        
        $query = $this->db->get_where('series_cliente', array('id_serial_cliente' => $id_serial_cliente));
        $query = $query->result_array();
        $id_serial_cliente = $query[0]['serie'];
        
        $data = array(
            'cliente'=> $id_clientes,
            'serie' => $id_serial_cliente,
        );
        
        $this->db->where('id_tarea', $lastid);
        $this->db->update('tarealog', $data);     
               
        return;
    }

 

for my callback:

 

$crud->callback_after_insert(array($this,'clienteyserie'));

 

Best regards great Forum