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).
Take a look at the way you can specify a custom option for a product (Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Options_Option
) and the assigned template file (catalog/product/edit/options/option.phtml
in adminhtml). Here you can see how to setup a row (var firstStepTemplate
) that and the needed javascript to add/remove rows.
Best Answer
EDIT: The entire answer updated, to accommodate new cms page creation.
Ok, you can solve this in multiple ways, rewrites are one (rewriting controller and models of the cms system (yuk)) or just using observers/events. I prefer observer events, as it makes for higher compatibility with other 3rd party modules.
As per my previous answer, the flaw was that I suggested using only the 'cms_page_prepare_save' event, but as you had found that does not work for new pages. Why? Because the page model has not saved yet, thus no page id.
However, this event is the only event available that also passes the request data, so it must be used.
The solution: Use two events. The first stays as 'cms_page_prepare_save', which allows you to get the request data, and the second will be an event on the model save after 'cms_page_save_after'.
Thus the process will be:
So here goes:
In the observer event 'cms_page_prepare_save' you will have code like this:
Next you have another observer on 'cms_page_prepare_save' event with this code:
Ideally in the cms_page_prepare_save observer code you'd only store your tabs information, not the entire request params, as that could be a lot of data. You could also use this event to filter/sanetise/check your data, before the models get saved.
Hope this adjusted answer helps.