读过Alan Storm 的Fixing Magento Flat Collections with Chaos并在这里找到类似的问题后,我试图返回属于某个类别但不使用 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);
Run Code Online (Sandbox Code Playgroud)
当通过 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');
}
}
Run Code Online (Sandbox Code Playgroud)
和:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
然后更改我的查询代码:
$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);
Run Code Online (Sandbox Code Playgroud)
然而上面的代码仍然查询平面数据表。我可能做错了什么?
小智 3
因为你已经重写了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()];
}
Run Code Online (Sandbox Code Playgroud)
到
public function isEnabledFlat()
{
return false;
}
Run Code Online (Sandbox Code Playgroud)
这应该可以解决它:)
| 归档时间: |
|
| 查看次数: |
4820 次 |
| 最近记录: |