I needed to grab a few category names when all I had were the category IDs
$categories = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('name')
->addFieldToFilter('category_id', array('in'=>$product['category_ids']));
Can you spot the error? PHP complained with the following error
Fatal error: Call to a member function getBackend() on a non-object in /Users/alanstorm/Sites2014/magento-1-9-0-0.dev/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 816
Can you spot the error now? Unless you happen to be working on a project where you’re deep inside Magento’s EAV implementation, the above error message is worse than useless when it comes to tracking down the actual error.
The problem was I used category_id
— and a Magento category object has no such field. I should have used entity_id
. Easy brain fart — made easier in that Magento doesn’t stick to the entity_id
convention.
Why such a cryptic error message? Because somewhere along the line Magento’s EAV code checks if the filter attribute is or is not a static attribute.
#File: app/code/core/Mage/Eav/Model/Entity/Abstract.php
public function isAttributeStatic($attribute)
{
$attrInstance = $this->getAttribute($attribute);
$attrBackendStatic = $attrInstance->getBackend()->isStatic();
return $attrInstance && $attrBackendStatic;
}
Magento tried to load a category_id
attribute
$attrInstance = $this->getAttribute('category_id');
This failed because there’s no such attribute. Magento returned false
. Then Magento tried to call a method on something it expected to be an object, but ended up being a boolean.
$attrInstance->getBackend()
That’s why we got the cryptic error message. A certified or experienced developer will be able to track this sort of thing down — but to a newcomers cryptic error messages are kryptonite.