Do not use "flat" data in magento

Do not use "flat" data in magento

August 15, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Do not use "flat" data in magento

Magento’s flat data can be a double-edged sword - it speeds up catalog browsing but can cause issues when trying to retrieve complex product information. In this blog post, we’ll explore the challenges of using “flat” data in Magento and provide solutions for working with EAV models instead.

The Problem with Flat Data

Magento’s flat data was introduced to optimize performance by pre-calculating data relationships and storing them in a single table. While this works well for simple catalog browsing, it can cause difficulties when trying to retrieve complex product data or make changes that require access to the original EAV (Entity-Attribute-Value) model.

You’ve noticed this issue yourself - using flat data produces different results depending on whether your code is run through AJAX or an observer. This inconsistency happens because Magento uses the flat data by default for performance reasons, even when you’re trying to work with the EAV model directly.

Why Use EAV Models?

For advanced customizations and complex queries, it’s often necessary to work with the EAV models rather than relying on flat data:

  • Complex Queries: EAV allows you to query across multiple tables and attributes more easily.
  • Data Consistency: Changes made through EAV models are immediately reflected in all relevant places.
  • Flexibility: You have full control over how data is retrieved and manipulated.

How to Use EAV Models Instead of Flat Data

To work with EAV models effectively, you need to override the default Magento behavior. Here’s a step-by-step guide:

  1. Override the Category Model Create a new class that extends Mage_Catalog_Model_Category and initialize it using the correct resource model.

    app/code/local/Mynamespace/Mymodule/Model/Category.php:
    class Mynamespace_Mymodule_Model_Category extends Mage_Catalog_Model_Category
    {
        protected function _construct()
        {
            $this->_init('catalog/category');
        }
    }
    
  2. Override the Product Collection Create a new product collection class that extends Mage_Catalog_Model_Resource_Product_Collection and initializes it using the correct table.

    app/code/local/Mynamespace/Mymodule/Model/Productcollection.php:
    class Mynamespace_Mymodule_Model_Productcollection extends Mage_Catalog_Model_Resource_Product_Collection
    {
        protected function _construct()
        {
            $this->_init('catalog/product');
            $this->_initTables();
        }
    }
    
  3. Use the Custom Models In your code, use Mage::getModel('mymodule/category') and Mage::getModel('mymodule/productcollection') instead of the default models.

    $category_model = Mage::getModel('mymodule/category')->load($cid);
    $collection = Mage::getModel('mymodule/productcollection');
    
  4. Avoid Using Flat Tables Ensure your queries do not directly access flat tables like catalog_product_flat_1. If you need to access these tables, use Magento’s indexing and reindexing mechanisms instead.

Conclusion

Working with EAV models in Magento requires a strategic approach - you must override the default behavior and carefully manage how data is retrieved. While it requires more effort than using flat data, it provides the flexibility necessary for complex customizations and ensures consistency across your entire store.

Remember that Magento’s “flat” feature is an optimization tool, not a replacement for the EAV model. For most advanced operations, you should always default to working with EAV models directly.

For further reading on optimizing Magento performance without relying on flat data, check out our post Maximizing Magento Performance Without Flat Data at LaravelCompany.

Happy coding!

Catalog Database Performance Extensions

Our network