Categories


Archives


Recent Posts


Categories


Call to a member function getBackend() on a non-object

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

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.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 16th July 2014

email hidden; JavaScript is required