Do not use flat data in magento

After reading Alan Storm's Fix for Magento Flat Collections with Chaos and finding a similar SO question here, I'm trying to return products that are in a category but without using Magento flat data.

Here is the code I originally used:

        $category_model = Mage::getModel('catalog/category')->load($cid);
        $collection = Mage::getModel('catalog/product_collection');
        $collection->addCategoryFilter($category_model);
        $collection->addAttributeToSort('entity_id','DESC'); 
        $collection->addAttributeToSelect('*');

        $collection->printLogQuery(true);

When this code runs through AJAX, I get different results than when I run it from an observer, and the reason is related to flat data. Therefore, I wrote my own classes designed to use the EAV 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');

    }

}

and

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();
    }

}

And then change my request code:

        $category_model = Mage::getModel('mymodule/category')->load($cid);
        $collection = Mage::getModel('mymodule/productcollection');
        $collection->addCategoryFilter($category_model);
        $collection->addAttributeToSort('entity_id','DESC'); 
        $collection->addAttributeToSelect('*');

        $collection->printLogQuery(true);

However, the above code still queries the flat data table. What can i do wrong?

+4
source share
2

Mage_Catalog_Model_Resource_Product_Collection

public function isEnabledFlat()
{
    if (Mage::app()->getStore()->isAdmin()) {
        return false;
    }
    if (!isset($this->_flatEnabled[$this->getStoreId()])) {
        $this->_flatEnabled[$this->getStoreId()] = $this->getFlatHelper()
            ->isEnabled($this->getStoreId());
    }
    return $this->_flatEnabled[$this->getStoreId()];
}

public function isEnabledFlat()
{
    return false;
}

:)

+3

, :

Mage::app()->getStore()->setConfig(
    Mage_Catalog_Helper_Product_Flat::XML_PATH_USE_PRODUCT_FLAT, '0');

, , , , ( isEnabledFlat().

+1

Source: https://habr.com/ru/post/1526332/


All Articles