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).
First of all...nice question. Got me really interested.
Here is an example on how you can create an attribute that has a custom renderer (template) and the value is saved serialized.
For this example I created an attribute that will have 2 text inputs, but you can basically put anything in there.
I recommend creating a custom module to handle your attribute.
Let's call this extension Easylife_Attr
.
You will need the following files.
app/etc/module/Easylife_Attr.xml
- the declaration file.
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Attr>
<codePool>local</codePool>
<active>true</active>
<depends>
<Mage_Catalog /><!-- should depend on mage_catalog-->
<Mage_Adminhtml /><!-- should depend on mage_adminhtml-->
</depends>
</Easylife_Attr>
</modules>
</config>
app/code/local/Easylife/Attr/etc/config.xml
- the configuration file
<?xml version="1.0"?>
<config>
<modules>
<Easylife_Attr>
<version>0.0.1</version>
</Easylife_Attr>
</modules>
<global>
<resources>
<easylife_attr_setup>
<setup>
<module>Easylife_Attr</module>
<class>Mage_Catalog_Model_Resource_Setup</class><!-- use the catalog setup so you can add your attribute -->
</setup>
</easylife_attr_setup>
</resources>
<models>
<easylife_attr>
<class>Easylife_Attr_Model</class>
</easylife_attr>
</models>
<blocks>
<easylife_attr>
<class>Easylife_Attr_Block</class>
</easylife_attr>
</blocks>
</global>
<adminhtml>
<events>
<adminhtml_catalog_product_edit_prepare_form><!-- event needed to add a template to a certain attribute -->
<observers>
<easylife>
<class>Easylife_Attr_Model_Observer</class>
<method>convertCustomValues</method>
</easylife>
</observers>
</adminhtml_catalog_product_edit_prepare_form>
</events>
</adminhtml>
</config>
app/code/local/Easylife/Attr/sql/easylife_attr_setup/install-0.0.1.php
- the install script. It will add your attribute
<?php
$this->addAttribute('catalog_product', 'custom_values', array(
'group' => 'Custom values', //the tab name where the attribute will be placed
'input' => 'textarea', //this is not really important
'type' => 'text', //attribute type should be text to support long values
'label' => 'Custom values', //the attribute label
'backend' => 'easylife_attr/custom', //a custom backend model that will handle serialization and deserialization
'visible' => true,
'required' => false,
'visible_on_front' => true,
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL, //scope can be anything but if it's not global you will need some changes to support store view values
));
Now the fun part.
app/code/local/Easylife/Attr/Model/Observer.php
- the observe that will change the attribute template
<?php
class Easylife_Attr_Model_Observer {
public function convertCustomValues($observer) {
$form = $observer->getEvent()->getForm();
$customValues = $form->getElement('custom_values');
if ($customValues) {
$customValues->setRenderer(
Mage::app()->getLayout()->createBlock('easylife_attr/adminhtml_product_custom')
); //set a custom renderer to your attribute
}
}
}
app/code/local/Easylife/Attr/Block/Adminhtml/Product/Custom.php
- the custom renderer block
<?php
class Easylife_Attr_Block_Adminhtml_Product_Custom
extends Mage_Adminhtml_Block_Widget
implements Varien_Data_Form_Element_Renderer_Interface {
public function __construct()
{
$this->setTemplate('easylife_attr/product/custom.phtml'); //set a template
}
public function render(Varien_Data_Form_Element_Abstract $element) {
$this->setElement($element);
return $this->toHtml();
}
}
app/design/adminhtml/default/default/template/easylife_attr/product/custom.phtml
- the template for the attribute.
<?php
$_htmlId = $this->getElement()->getHtmlId();
$_htmlClass = $this->getElement()->getClass();
$_htmlName = $this->getElement()->getName();
$_readonly = $this->getElement()->getReadonly();
$value = $this->getElement()->getValue();
//get the values for the 2 text elements
$someField = (isset($value['some_field']) ? $value['some_field'] : '');
$otherField = (isset($value['other_field']) ? $value['other_field'] : '');
?>
<tr> <!-- should be wrapped in a tr element to fit in the admin template -->
<td class="label"><?php echo $this->getElement()->getLabel(); ?></td>
<td colspan="10" class="grid">
<table cellspacing="0" class="data border">
<col width="120" />
<col />
<thead>
<tr class="headings">
<th><?php echo $this->__('Some field')?></th>
<th><?php echo $this->__('Other Field')?></th>
</tr>
</thead>
<tbody id="<?php echo $_htmlId; ?>_container"></tbody>
<tfoot>
<tr>
<td><input type="text" name="<?php echo $_htmlName; ?>[some_field]" value="<?php echo $someField?>"<?php echo ($_readonly) ? ' readonly="readonly"' : ''?>> </td>
<td><input type="text" name="<?php echo $_htmlName; ?>[other_field]" value="<?php echo $otherField?>"<?php echo ($_readonly) ? ' readonly="readonly"' : ''?>></td>
</tr>
</tfoot>
</table>
</td>
</tr>
app/code/local/Easylife/Attr/Model/Custom.php
- the model that handles the serialization and deserialization.
<?php
class Easylife_Attr_Model_Custom extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract{
public function beforeSave($object)
{
//before sabing the product check if the attribute `custom_values` is array.
//if it is, serialize it for saving in the db
$attributeCode = $this->getAttribute()->getAttributeCode();
$data = $object->getData($attributeCode);
if (is_array($data)) {
$data = array_filter($data);
$object->setData($attributeCode, serialize($data));
}
return parent::beforeSave($object);
}
public function afterLoad($object) {
//after loading the product, check if the value for custom_values is not an array. If it's not try to unserialize the value.
$attributeCode = $this->getAttribute()->getAttributeCode();
$data = $object->getData($attributeCode);
if (!is_array($data)) {
$object->setData($attributeCode, @unserialize($data));
}
return parent::afterLoad($object);
}
}
That's it. Here is how the attribute would look in the backend:
Best Answer
I think you should follow the same path which Magento follow in module
CMS
for uniquecms block identifier
and uniquecms page url
, you can add_beforeSave
method in yourResourceModel
class and check your unique value.In CMS block you can find this,
File path
vendor\magento\module-cms\Model\ResourceModel\Block.php
From here you can get your starting point