Skip to content

Magento Models: Standard vs. EAV

Because Magento models include so many useful, extensible methods, it can be easy to forget that there are two quite different kinds of Magento models. There are basic, one model/one table models-these are fairly easy to create and maintain, and almost all of the “How to create a Magento Model” tutorials out there will show you how to create one of these. But there are also Entity Attribute Value Models, which have their object data spread across multiple, “tall and skinny” database tables, and though the two models share some methods, they differ in some important, easy to forget ways. For example, I wrote the following code to grab a Magento Collection of all customer models in a particular customer group that were created more than three days ago:

$three_days_ago = Mage::getModel('core/date')->date('Y-m-d H:i:s',strtotime('-72 hours'));
$collection = Mage::getModel('customer/customer')->getCollection()
    ->addAttributeToSelect( '*' )
    ->addFieldToFilter( 'group_id','6')
    ->addFieldToFilter('created_at',array('lt'=>$three_days_ago));

I then re-used this code in a custom module. But I had forgotten that customer/customer is an EAV model and the model in my custom module was basic. Since the addAttributeToSelect method is only available for EAV models, the code didn’t work until I removed it. It’s easy to forget about the two distinct kinds of Magento models, as for many applications they function identically. The mistake was difficult to find.

As usual, Alan Storm has the best explanation of the difference between the two Magento model flavors and a discussion of how to create an EAV model of your own, located here.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*