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).
To use an attribute in the frontend you have to do two things.
- add the attribute to the EAV
tell magento that the attribute should be used and is allowed to be saved by the customer. There are lots of attributes you don't want to be saved by the customer, for example created_at, updated_at or any account balance should not be editable by the customer.
All the attributes are saved in the different forms via
$customerForm = Mage::getModel('customer/form');
$customerForm->setFormCode('customer_account_create')
->setEntity($customer);
Have a look on the form codes. They are important to change the attributes. there are a few, like customer_account_edit
, customer_account_create
. Just check the code if the forms doesn't save your attribute, what is needed and how the form is named.
Add the attribute
<?php
/* @var $installer Mage_Catalog_Model_Resource_Setup */
$installer = $this;
$vCustomerEntityType = $installer->getEntityTypeId('customer');
$vCustAttributeSetId = $installer->getDefaultAttributeSetId($vCustomerEntityType);
$vCustAttributeGroupId = $installer->getDefaultAttributeGroupId($vCustomerEntityType, $vCustAttributeSetId);
$installer->startSetup();
$attributes = array(
'website' => 'Website',
);
foreach ($attributes as $name => $attribute) {
$installer->addAttribute(
'customer',
$name,
array(
'label' => $attribute,
'input' => 'text',
'type' => 'varchar',
'required' => 0,
'user_defined' => 1,
)
);
$installer->addAttributeToGroup($vCustomerEntityType, $vCustAttributeSetId, $vCustAttributeGroupId, $name, 0);
Make attribute usable in forms
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', $name);
$oAttribute->setData(
'used_in_forms',
array('customer_account_edit', 'customer_account_create', 'adminhtml_customer')
);
$oAttribute->save();
}
$installer->endSetup();
Best Answer
If you wants to insert data you can try below way,
Insert Custom Query.