About a year ago, I noticed some category or product attributes, for example, sku, path, etc., were given backend type “static” in class Mage_Catalog_Model_Resource_Eav_Mysql4_Setup. But I failed to find out what static meant here, or I did not know what the difference was between static and datetime/decimal/int/text/varchar. All I knew at that time was if I gave my user defined attribute a “static” type, the attribute values were stored in _varchar table.
Recently, I dived deeper into Magento EAV module. Now I can answer my question.
An entity can have some “static” attributes, whose values are stored in entity main table. Take sku for example, sku values are stored in catalog_product_entity table. Although sku is varchar type, its values are not stored in catalog_product_entity_varchar. Magento does not lookup catalog_product_entity_varchar for sku values because in eav_attribute table, sku backend type is defined as “static”.
It also explains why I can get sku values without add it to select, but I can not do the same with non static attributes.
$collection = Mage::getModel('catalog/product')->getCollection(); echo $collection->getFirstItem()->getSku();
It will output the sku.
$collection = Mage::getModel('catalog/product')->getCollection(); echo $collection->getFirstItem()->getName();
It will not output the name.
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('name'); echo $collection->getFirstItem()->getName();
Calling addAttributeToSelect is a must to retrieve non static attribute values.
For user defined attributes of existing entity, if I do not change the structure of main entity table, even I specify backend as “static” in entity setup class, Magento has nowhere to store their values to but fallback to _varchar table.
Thank you very much for the nice explanation
You are welcome.