⚠ 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

How to insert data into two tables by update and insert



alex646
  • profile picture
  • Member

Posted 13 June 2013 - 03:37 AM

Hey guys,

 

 

I would appreciate if somebody could help me with the follwoing problem:

 

I have two tables, posts and users.

 

I want to insert data into these related tables at the same time when the post

is being updated or new post is added.  The problem I have now is that I can only insert data into posts table, but I also

need to update users table by adding a post id to users.post_id column.  Can this be done during update and insert at the same time?

 

Thanks for your help!

 


victor
  • profile picture
  • Member

Posted 13 June 2013 - 07:49 AM

Use callback_after_insert and callbak_after_update functions


victor
  • profile picture
  • Member

Posted 13 June 2013 - 07:50 AM

learn this API list: 

http://www.grocerycrud.com/documentation/options_functions

 


davidoster
  • profile picture
  • Member

Posted 13 June 2013 - 07:58 AM

Isn't this question the same with this one?


alex646
  • profile picture
  • Member

Posted 13 June 2013 - 13:41 PM

Isn't this question the same with this one?

That question was resolved thanks for that davidoster!


alex646
  • profile picture
  • Member

Posted 13 June 2013 - 15:19 PM

Use callback_after_insert and callbak_after_update functions

Thanks Victor!

 

I'll take a look at the apis


alex646
  • profile picture
  • Member

Posted 14 June 2013 - 02:35 AM

Hi

victor,

 

I couldn't make it to work. I've tried the callback_before_insert, but didn't know how to handle it properly.

I have two tables, one posts and the other users, I need to insert post and also at the same time insert post's id number to users table/field. I got

stuck with how to go about it. I would appreciate if someone can help me with this.

 

Thanks!


davidoster
  • profile picture
  • Member

Posted 15 June 2013 - 07:08 AM

On callback_after_insert you have something like this,

 

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

 

and then this

 

function log_user_after_insert($post_array,$primary_key)
{
$user_logs_insert = array(
"user_id" => $primary_key,
"created" => date('Y-m-d H:i:s'),
"last_update" => date('Y-m-d H:i:s')
);
 
$this->db->insert('user_logs',$user_logs_insert);
 
return true;
}

 

 

the $post_array holds all the information the user has typed and stored to the database.

You can get any of these data like $post_array['username'] and post htese data to the other table as above.


alex646
  • profile picture
  • Member

Posted 16 June 2013 - 03:31 AM

On callback_after_insert you have something like this,

 

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

 

and then this

 

function log_user_after_insert($post_array,$primary_key)
{
$user_logs_insert = array(
"user_id" => $primary_key,
"created" => date('Y-m-d H:i:s'),
"last_update" => date('Y-m-d H:i:s')
);
 
$this->db->insert('user_logs',$user_logs_insert);
 
return true;
}

 

 

the $post_array holds all the information the user has typed and stored to the database.

You can get any of these data like $post_array['username'] and post htese data to the other table as above.

Thanks Man! it worked perfectly!


Anni
  • profile picture
  • Member

Posted 26 November 2013 - 22:45 PM

Hey,

 

I have the same problem. I want to insert data into related tables with callback_after_insert function, but it does not work.

 

Does anyone have an idea?

 

  
function gebaeude(){
$crud = new grocery_CRUD();
$crud->set_theme('datatables');
$crud->set_table('gebaeude');
$crud->set_relation_n_n('Fassade', 'ht_3', 'fassade', 'Gebaeude_Gebaeude_ID', 'Fassade_Fassade_ID', 'Fassade_ID'); 
$crud->callback_after_insert(array($this,'fs_gebaeude')); 
$output = $crud->render(); 
$this->load->view('view', $output);} 

function fs_gebaeude($post_array, $primary_key){ 
$g = $post_array['Gebaeude_ID']; 
$f = $post_array['Fassade']; 
$data = array( 
'Gebaeude_Gebaeude_ID' => $g, 
'Fassade_Fassade_ID' => $f); 
$this->db->insert('ht_3', $data); 
return true;}