Disable usage of flat tables for categories via code

<?php
$oCategoryCollection = Mage::getModel('catalog/category', array('disable_flat' => true))->getCollection();

That’s it! Now the “normal” entity tables are used.

That work’s because the field disable_flat decides what kind of collection is loaded in the constructor of Mage_Catalog_Model_Category:

<?php 
class Mage_Catalog_Model_Category extends Mage_Catalog_Model_Abstract
{
//...

    /**
     * Initialize resource mode
     *
     * @return void
     */
    protected function _construct()
    {
        // If Flat Data enabled then use it but only on frontend
        $flatHelper = Mage::helper('catalog/category_flat');
        if ($flatHelper->isAvailable() && !Mage::app()->getStore()->isAdmin() && $flatHelper->isBuilt(true)
            && !$this->getDisableFlat() ########### That's the point where magic happens! #######
        ) {
            $this->_init('catalog/category_flat');
            $this->_useFlatResource = true;
        } else {
            $this->_init('catalog/category');
        }
    }

//..
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.