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?
source
share