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:
class Mage_Eav_Model_Entity_Attribute_Backend_Store extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
protected function _beforeSave($object)
if (!$object->getData($this->getAttribute()->getAttributeCode())) {
$object->setData($this->getAttribute()->getAttributeCode(), Mage::app()->getStore()->getId());
  • 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:
class Mage_Eav_Model_Entity_Attribute_Frontend_Datetime extends Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
public function getValue(Varien_Object $object)
$data = '';
$value = parent::getValue($object);
$format = Mage::app()->getLocale()->getDateFormat(

if ($value) {
try {
$data = Mage::getSingleton('core/locale')->date($value, Zend_Date::ISO_8601, null, false)->toString($format);
} catch (Exception $e) {
$data = Mage::getSingleton('core/locale')->date($value, null, null, false)->toString($format);

return $data;
  • 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.
class Mage_Eav_Model_Entity_Attribute_Source_Boolean extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
public function getAllOptions()
if (is_null($this->_options)) {
$this->_options = array(
'label' => Mage::helper('eav')->__('Yes'),
'value' => 1
'label' => Mage::helper('eav')->__('No'),
'value' => 0
return $this->_options;


public function getOptionText($value)
$options = $this->getAllOptions();
foreach ($options as $option) {
if ($option['value'] == $value) {
return $option['label'];
return false;

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:
$setup = new Mage_Eav_Model_Entity_Setup('catalog_setup');
$attr = array(
'group' => 'Prices',
'type' => 'text',
'input' => 'textarea',
'label' => 'Gift amount',
'backend' => '',
'frontend' => '',
'source' => '',
'visible' => 1,
'user_defined' => 1,
'used_for_price_rules' => 1,
'position' => 2,
'unique' => 0,
'default' => '',
'sort_order' => 101,
  • In case you want to update an attribute, you are able to use the model to change it. For example:
$setup = new Mage_Eav_Model_Entity_Setup('catalog_setup');
$giftAmount = Mage::getModel('catalog/resource_eav_attribute')->load($setup->getAttributeId('catalog_product','gift_amount'));
'is_global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'is_required' => 0,
'apply_to' => array('giftvoucher'),
'is_configurable' => 1,
'is_searchable' => 1,
'is_visible_in_advanced_search' => 1,
'is_comparable' => 0,
'is_filterable' => 0,
'is_filterable_in_search' => 1,
'is_used_for_promo_rules' => 1,
'is_html_allowed_on_front' => 0,
'is_visible_on_front' => 0,
'used_in_product_listing' => 1,
'used_for_sort_by' => 0,
'backend_type' => 'text',
  • 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:
* Update Attribute data and Attribute additional data
* @param mixed $entityTypeId
* @param mixed $id
* @param string $field
* @param mixed $value
* @param int $sortOrder
* @return Mage_Eav_Model_Entity_Setup
public function updateAttribute($entityTypeId, $id, $field, $value=null, $sortOrder=null)
$this->_updateAttribute($entityTypeId, $id, $field, $value, $sortOrder);
$this->_updateAttributeAdditionalData($entityTypeId, $id, $field, $value);
return $this;

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 😉


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

Write A Comment