Here I come to know about an awesome feature of Magento. When we add any eav attribute to an entity we can give a backend model name. Whenever Entity will be loaded magento initiates Mage_Eav_Model_Entity_Attribute model for each attribut. If it finds any backend model name for a eav attribut, it loads the corresponding model and based on what it is doing it will call different method from that model.

Here I am giving a complete example how you can use this feature. We will add an attribue to address model as “address_verified” , use a backend model to set this value whenever the any address will be save.

Step 1.Create a custom model “Mymodule”

Step 2.Write an sql setup for this module. It will add an “address_verified” attribute to address entity

<?php $installer = $this; $installer->startSetup();

$installer->run("
INSERT INTO {$this->getTable('eav_attribute')}  (
`entity_type_id` ,
`attribute_code` ,
`attribute_model` ,
`backend_model` ,
`backend_type` ,
`backend_table` ,
`frontend_model` ,
`frontend_input` ,
`frontend_input_renderer` ,
`frontend_label` ,
`frontend_class` ,
`source_model` ,
`is_global` ,
`is_visible` ,
`is_required` ,
`is_user_defined` ,
`default_value` ,
`is_searchable` ,
`is_filterable` ,
`is_comparable` ,
`is_visible_on_front` ,
`is_html_allowed_on_front` ,
`is_unique` ,
`is_used_for_price_rules` ,
`is_filterable_in_search` ,
`used_in_product_listing` ,
`used_for_sort_by` ,
`is_configurable` ,
`apply_to` ,
`position` ,
`note` ,
`is_visible_in_advanced_search`
)
VALUES (
'2', 'is_validated', NULL , 'customer_entity/address_attribute_backend_validate', 'int', NULL , NULL , 'text', NULL , NULL , NULL , NULL , '1', '1', '0', '0', NULL , '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '', '', '', '0'
);

");

$installer->endSetup(); 

Step3.Write the back end model in your local code pool


<?php 

class Mypackage_Customer_Model_Entity_Address_Attribute_Backend_Validate
	extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
{
	public function beforeSave($object)
	{
		//Write your code to validate address
		$object->setIsValidated($satoriStatus);
		return $this;
    }
}

Step 4. Override the backend model so that it runs from your local pool

</config>
 <models>
 <customer_entity>
 <rewrite>
 <address_attribute_backend_satori>Mypackage_Customer_Model_Entity_Address_Attribute_Backend_Satori</address_attribute_backend_satori>
 </rewrite>
 </customer_entity>
 </models>
<config>
Advertisements