Mini Tutorial: eav_attribute Table in Magento

It is well known that Magento implements an entity-attribute-value data model. A key table in the implementation is the ‘eav_attribute’ table, who’s exposition is the subject of this blog post.

Magento uses five ‘types’ to store attribute data: datetime, decimal, int, text, and varchar (these are strongly motivated by the corresponding mySQL datatypes). So when a new eav entity type is created in Magento there are actually six tables created; one to store the entity and one for each of these ‘types’ of attribute data. The fact that this type of entity has a certain attribute is recorded in the ‘eav_attribute’ table which has the following schema:

CREATE TABLE 'eav_attribute' (
'attribute_id' smallint(5),
'entity_type_id' smallint(5),
'attribute_code' varchar(255),
'attribute_model' varchar(255),
'backend_model' varchar(255),
'backend_type' varchar(8),
'backend_table' varchar(255),
'frontend_model' varchar(255),
'frontend_input' varchar(50),
'frontend_label' varchar(255),
'frontend_class' varchar(255),
'source_model' varchar(255),
'is_required' smallint(5),
'is_user_defined' smallint(5),
'default_value' text,
'is_unique' smallint(5),
'note' varchar(255),
PRIMARY KEY ('attribute_id'),
UNIQUE KEY 'UNQ_EAV_ATTRIBUTE_ENTITY_TYPE_ID_ATTRIBUTE_CODE' ('entity_type_id','attribute_code'),
KEY 'IDX_EAV_ATTRIBUTE_ENTITY_TYPE_ID' ('entity_type_id')
);

‘attribute_id’ is simply a primary key for this table.

‘entity_type_id’ is a reference to the ‘eav_entity_type’ table and tells Magento what entity has the attribute in this record. Typical examples are ‘customer’ and ‘catalog_product’.

‘attribute_code’ is a developer-readable name we can use to retrieve the attribute. A typical use would be $eavModel->getResource()->getAttribute('my_attribute');. Note that the UNIQUE KEY statement in the schema guarantees that these codes will be unique for each entity.

‘backend_model’ is to do with how the entity is saved into the database; this class will extend Mage_Eav_Model_Entity_Attribute_Backend_Abstract.

‘backend_type’ is simply one of the five Magento eav datatypes (datetime, decimal, int, text, or varchar), or the value ‘static’ which means this attribute is stored in the parent entity table as opposed to one of the five auxillary tables.

‘frontend_model’ is a model class for use in displaying the attribute on the front end. For example product images are stored as ‘varchar’ attributes of the ‘catalog_product’ entity and their ‘frontend_model’ exposes a ‘getUrl’ method. The ‘frontend_model’ will extend Mage_Eav_Model_Entity_Attribute_Frontend_Abstract.

‘frontend_input’ describes the form element used to enter the attribute data in the admin section. This is related to the ‘backend_model’ field. If there’s nothing in the ‘backend_model’ field then I think this needs to identify one of the classes in the Mage_Eav_Model_Attribute_Data folder.

‘frontend_label’ is simply a human-readable label to help the administrator enter the attribute data in the back end.

‘frontend_class’ is a CSS class for the admin form.

‘source_model’ will extend Mage_Eav_Model_Entity_Attribute_Source_Abstract, or at the very least implement Mage_Eav_Model_Entity_Attribute_Source_Interface, in which “Source is providing the selection options for user interface”.

The rest are pretty self-explanatory:
‘is_required’
‘is_user_defined’
‘default_value’
‘is_unique’
‘note’