3

I have created a form in the admin panel with various fields. Now the fields here doesn't belong to one single table. On the save, I want some values to go into one particular table while others to go into some other table. I am able to show up the data using joins but don't know how to save them back.

Let's say I have a tblUser with fields:

tblUser

- user_id INT(11) Auto Increment
- username VARCHAR(15)
- store_id SMALLINT(5)
- bank_id INT(11)

Here, store_id and bank_id have foreign key constraints to auto-increment id's of tblcore_store (id, store_name) and tblBanks (id, bank_name, bank_acc) respectively. Now the fields in the form are:

  • Username
  • Store Id
  • Store Name
  • Bank Name
  • Bank Account

When admin click save I want the data of form to go into their respective tables and also having their references in store_id and bank_id.

Aasim Goriya
5,4622 gold badges30 silver badges54 bronze badges
asked Feb 1, 2013 at 8:04
1
  • Why you don't want to separate into simple insert/update queries? Commented Feb 1, 2013 at 13:30

1 Answer 1

4

You could select a main table (model and resource - in your case tblUser) that will do all the save operations. Then in the controller you should set all the data in that model and write your own _beforeSave function that will save all other models into other tables, add their ids to main table model and then save the main model.

_beforeSave function should be implemented in that model's resource model. Model/Resource/MainModule.php file:

...
// this function will get executed before the model is saved but after save function has been called
protected function _beforeSave( Mage_Core_Model_Abstract $object )
{
 // do some model loads and checks here so that you will update existing data and not duplicate rows on editAction saves...
 $otherModel = Mage::getModel( 'moduleName/otherTableModel' );
 $otherModel->setData( 'foo', $object->getData( 'other_table_foo' ) )->save();
 $object->setData( 'bank_id', $otherModel->getId() );
 return parent::_beforeSave( $object );
}
...

In your controller you will just have to write:

$mainModel = Mage::getModel( 'moduleName/mainModel' );
if( $id ) {
 $mainModel->load( $id );
}
$mainModel->setData( $dataFromServerRequest ) // or addData...
 ->save();

and the _beforeSave function will do the rest.

This will save the data that you want in other tables to other tables while still executing all the queries in the same transaction so they will be saved or rolled back all together.

This technique works because no matter what you add to a model with setData will be preserved after call to save function (and more importantly inside _afterSave and _beforeSave functions) but only the data that has the same name as a table column will actualy be saved. You just need to name values from other tables differently than the columns in the current model's table and you won't have any problems accessing that data in _beforeSave function and saving it to other tables.

All of this can also be done in _afterSave if you need to get $object->getId() and save it to a child table (in _beforeSave the $object hasn't been saved yet so the id is present only if that table entry existed before and was loaded. On the other hand in _afterSave it has already been saved so you can use it's unique id there).

answered Feb 1, 2013 at 15:36
1
  • Thanks a ton. I am kind of novice in Magento developtment and I guess you have actually saved me a lot. Commented Feb 2, 2013 at 6:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.