不要使用“;“平坦”;magento中的数据

不要使用“;“平坦”;magento中的数据,magento,Magento,在阅读了Alan Storm的文章并发现了一个类似的SO问题后,我试图退回属于某一类别但未使用Magento“平面”数据的产品 以下是我最初使用的代码: $category_model = Mage::getModel('catalog/category')->load($cid); $collection = Mage::getModel('catalog/product_collection'); $collection->addC

在阅读了Alan Storm的文章并发现了一个类似的SO问题后,我试图退回属于某一类别但未使用Magento“平面”数据的产品

以下是我最初使用的代码:

        $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);
当这段代码通过AJAX触发时,我得到的结果与从观察者处运行它时不同,原因是由于平面数据。因此,我编写了自己的类,打算使用EAV模型:

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');

    }

}
以及:

然后更改我的查询代码:

        $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);

但是,上面的代码仍在查询平面数据表。我可能做错了什么?

因为您已经重写了
Mage\u Catalog\u Model\u Resource\u Product\u 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()];
}


这应该可以解决它:)

要禁用当前请求的平面索引,您还可以临时设置配置而不保存它:


在产品集合初始化之前完成此操作非常重要,因为这是检查配置的地方(通过
isEnabledFlat()

编写我自己的SQL查询,暂时避免了这一点,但如果有人能够提供帮助,这将非常有用。
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;
}
Mage::app()->getStore()->setConfig(
    Mage_Catalog_Helper_Product_Flat::XML_PATH_USE_PRODUCT_FLAT, '0');