A model's collection turns out to be a non-object

A model's collection turns out to be a non-object

September 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
A model's collection turns out to be a non-object

Resolving “Fatal error: Call to a member function getBackend() on a non-object” in Magento

Dear developers,

I hope this post finds you well. Today, let’s dive into an issue many of us have faced while working with Magento collections - the dreaded “Call to a member function getBackend() on a non-object”. Let me guide you through understanding and resolving this problem.

The Problem: Non-Object Collections

You were trying to fetch viewed products using the reports/product_index_viewed model:

$model = Mage::getModel('reports/product_index_viewed')->getCollection()
    ->addAttributeToFilter('store_id', array('eq' => 1));

But you encountered this fatal error:

Fatal error: Call to a member function getBackend() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 816

The issue lies with the collection you’re trying to manipulate. The getCollection() call doesn’t always return an object that supports EAV (Entity-Attribute-Value) operations.

Understanding EAV and Non-EAV Models

Magento uses a combination of flat and EAV tables for storing data. Some models are built on top of EAV, while others aren’t. The reports/product_index_viewed model is a non-EAV model.

Non-EAV models don’t have an associated EAV entity ID or entity type ID, which the getBackend() method expects to find in the object’s metadata. When you try to use this method on a non-EAV collection, you hit the fatal error.

Resolving the Issue

To fix this problem, you need to ensure that your model is actually EAV-capable before performing EAV-related operations:

$model = Mage::getModel('reports/product_index_viewed');

if ($model->hasData('entity_type_id')) {
    $collection = $model->getCollection()
        ->addAttributeToFilter('store_id', array('eq' => 1));
} else {
    // Handle non-EAV models differently
}

In this updated code, I’ve added a check ($model->hasData('entity_type_id')) to verify if the model has an entity type ID before attempting to fetch the collection. If it doesn’t have this data, we know it’s not EAV-capable.

Best Practices for Working with Collections

To avoid such issues in the future:

  1. Always check if a model is EAV-capable before applying EAV-related methods.
  2. Familiarize yourself with the specific models and their relationships to avoid unexpected behavior.
  3. Use Magento’s built-in tools, like the Magento Developer Toolkit or the Database Structure tool in Magento Admin, to understand your database schema.

Remember, knowledge of the underlying data structure is key to working effectively with Magento collections.

Conclusion

The “Call to a member function getBackend() on a non-object” error highlights an important aspect of Magento’s architecture - the distinction between EAV and non-EAV models. By understanding this difference and implementing proper checks before manipulating collections, you can prevent such errors from occurring.

If you found this explanation helpful or have any further questions about working with Magento collections, feel free to reach out in the comments below. Your insights are always welcome!

Until next time, [Your Name]

P.S. For more advanced topics on Magento development, be sure to check out our Magento tutorials and guides at Laravel Company.

Catalog Database Admin SEO

Our network