⚠ 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

Inserting data to multiple tables



Vanesh Mali

Vanesh Mali
  • profile picture
  • Member

Posted 09 January 2015 - 10:45 AM

Hi Everyone, I want to insert data from a single crud form, some fields value must get inserted into 1 table and some fields value must get inserted to 2nd table.

 

More Illustration:

 

I have 1 table:  Items

 and another : Items_photos

 

i succeed in inserting into items info into items. from the same form there is 1 field i want to add is "insert photos of items", this entry must be go to another table called "Items_photos".

Same things must be done in case of add.

 

 

Summary: 

In short, i want to get data from two tables, insert into two tables, update two tables at a time.

 

Any solution?

 


Amit Shah

Amit Shah
  • profile picture
  • Member

Posted 10 January 2015 - 04:41 AM

Well u surely can do it in the following manner:

Step 1 - Set the add / edit fields (add the extra fields apart from the items table that u want to enter directly into the photos)

Step 2 - add a callback_before_insert

In this - pull out the values of the extra fields from POST_ARRAY (the param in the function) and store the values in cache (I tried working with sessions - it failed for me so i prefered using cache.. if it works 4 u.. great go ahead and store them in session)

Once the values are plucked - just remove the keys (extra fields) from POST_ARRAY and return the same newly updated array. This is very important because if we dont do this - it will give error saying field not found.

Step 3 - Create a callback_after insert. .. here u will get the primary key for the newly inserted record.... retrieve the previously stored value (from cache / session whatever u used for the same). Start storing them into another table using the primary key to link the data.

 

Sit back.. think up .. have shared u the complete logic.. dont ask for the code :)

 

Happy GCing :)