Magento is designed to work with EAV database model and an attribute management is the indispensable part for developers to customize the system. I spent quite long time on researching on EAV Attribute Management and today I am very delighted to share my experience and knowledge of this important issue with you. I will also guide you how to create and customize an attribute by your code.

1. EAV Attribute

EAV attribute in Magento is stored quite complicatedly (as we mentioned in the previous post). But Magento provides some interfaces in order to work with EAV attribute easily. There are some needed models for an EAV attribute:

  • Attribute model: extends from class Mage_Eav_Model_Entity_Attribute_Abstract, provides functions to work with the EAV attribute.
  • Backend model: extends from class Mage_Eav_Model_Entity_Attribute_Backend_Abstract, provides functions to work directly with database data. It is used to convert the custom type to the database type (for storage) and vice versa (to be compatible with entity attribute value in the object). The most important function of this class is to guarantee data is stored correctly. For example:
1class Mage_Eav_Model_Entity_Attribute_Backend_Store extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
2{
3protected function _beforeSave($object)
4{
5if (!$object->getData($this->getAttribute()->getAttributeCode())) {
6$object->setData($this->getAttribute()->getAttributeCode(), Mage::app()->getStore()->getId());
7}
8}
9}
  • Frontend model: extends from class Mage_Eav_Model_Entity_Attribute_Frontend_Abstract, provides functions to work with the frontend. It is used for the user interface programming. For instance:
01class Mage_Eav_Model_Entity_Attribute_Frontend_Datetime extends Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
02{
03public function getValue(Varien_Object $object)
04{
05$data = '';
06$value = parent::getValue($object);
07$format = Mage::app()->getLocale()->getDateFormat(
08Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM
09);
10 
11if ($value) {
12try {
13$data = Mage::getSingleton('core/locale')->date($value, Zend_Date::ISO_8601, null, false)->toString($format);
14} catch (Exception $e) {
15$data = Mage::getSingleton('core/locale')->date($value, null, null, false)->toString($format);
16}
17}
18 
19return $data;
20}
21}
  • Source model: extends from class Mage_Eav_Model_Entity_Attribute_Source_Abstract, provides the data source for an attribute. It is usually used for an option attribute. And a source model needs the override function getAllOptions() and getOptionText() for an attribute model to get all available options and option text.
01class Mage_Eav_Model_Entity_Attribute_Source_Boolean extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
02{
03public function getAllOptions()
04{
05if (is_null($this->_options)) {
06$this->_options = array(
07array(
08'label' => Mage::helper('eav')->__('Yes'),
09'value' => 1
10),
11array(
12'label' => Mage::helper('eav')->__('No'),
13'value' => 0
14),
15);
16}
17return $this->_options;
18}
19 
20..............
21 
22public function getOptionText($value)
23{
24$options = $this->getAllOptions();
25foreach ($options as $option) {
26if ($option['value'] == $value) {
27return $option['label'];
28}
29}
30return false;
31}
32}

2. Create and customize an attribute by your code

An EAV attribute in Magento can be managed easily in the backend. But when you need to create/update an attribute for your module, you can use some function provided by Magento. Magento provides the class Mage_Eav_Model_Entity_Setup for you to customize an EAV attribute.

  • If you want to add a new attribute, you can use function addAttribute() of the class above. Example:
01$setup = new Mage_Eav_Model_Entity_Setup('catalog_setup');
02$attr = array(
03'group' => 'Prices',
04'type' => 'text',
05'input' => 'textarea',
06'label' => 'Gift amount',
07'backend' => '',
08'frontend' => '',
09'source' => '',
10'visible' => 1,
11'user_defined' => 1,
12'used_for_price_rules' => 1,
13'position' => 2,
14'unique' => 0,
15'default' => '',
16'sort_order' => 101,
17);
18$setup->addAttribute('catalog_product','gift_amount',$attr);
  • In case you want to update an attribute, you are able to use the model to change it. For example:
01$setup = new Mage_Eav_Model_Entity_Setup('catalog_setup');
02$giftAmount = Mage::getModel('catalog/resource_eav_attribute')->load($setup->getAttributeId('catalog_product','gift_amount'));
03$giftAmount->addData(array(
04'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
05'is_required' => 0,
06'apply_to' => array('giftvoucher'),
07'is_configurable' => 1,
08'is_searchable' => 1,
09'is_visible_in_advanced_search' => 1,
10'is_comparable' => 0,
11'is_filterable' => 0,
12'is_filterable_in_search' => 1,
13'is_used_for_promo_rules' => 1,
14'is_html_allowed_on_front' => 0,
15'is_visible_on_front' => 0,
16'used_in_product_listing' => 1,
17'used_for_sort_by' => 0,
18'backend_type' => 'text',
19))->save();
  • And if you want to update an attribute value in the entity, it’s possible to use the function updateAttribute(). The prototype function is as below:
01/**
02* Update Attribute data and Attribute additional data
03*
04* @param mixed $entityTypeId
05* @param mixed $id
06* @param string $field
07* @param mixed $value
08* @param int $sortOrder
09* @return Mage_Eav_Model_Entity_Setup
10*/
11public function updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)
12{
13$this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
14$this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
15return $this;
16}

Well, I’ve given you the last part of the EAV model topic. The Adminhtml is also a very interesting issue and don’t forget to follow it in next tutorials on our blog.
Thanks for reading 😉

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment