Magento - addCategoryFilter 抛出错误
我成功运行了此查询:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setOrder('created_at', 'desc')
->setPage(1, 5);
但是当我添加时
->addCategoryFilter('3')
出现以下错误:
Fatal error: Call to a member function getId() on a non-object in /home/sitename/public_html/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php on line 556
它正在运行的块被定义为
<block type="catalog/product_list" name="catalog.right.bestsellers" template="catalog/navigation/right.phtml"/>
catalog.xml
I'm successfully running this query:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setOrder('created_at', 'desc')
->setPage(1, 5);
but when I add
->addCategoryFilter('3')
I got the following error:
Fatal error: Call to a member function getId() on a non-object in /home/sitename/public_html/app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php on line 556
The block it is running at is defined as
<block type="catalog/product_list" name="catalog.right.bestsellers" template="catalog/navigation/right.phtml"/>
at catalog.xml
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这涵盖了 1.4.2。根据您的错误,我认为您运行的版本稍旧,但错误的根本原因应该是相同的。
类别名
reports/product_collection
解析为资源模式上下文。
Mage_Reports_Model_Mysql4_Product_Collection
类是Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
的子类。Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
上的addCategoryFilter
看起来像这样注意,在 1.4.2 中,参数中进行了一些类型检查,
我怀疑您的版本没有这种检查。因此,当到达
You're pass in a string 时,它会失败,然后 Magento 尝试在该字符串上调用
getId
。这就是你收到错误的原因。Magento 希望您传入类别对象,而不是类别 ID。尝试一下
This covers 1.4.2. Based on your error, I think you're running a slightly older version, but the root cause of your error should be the same.
The class alias
reports/product_collection
resolves toin resource mode context. The class
Mage_Reports_Model_Mysql4_Product_Collection
is a child ofMage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
. TheaddCategoryFilter
onMage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
looks like thisNotice that in 1.4.2 there's some type checking going on in the paramaters
I suspect your version doesn't have that checking. Therefore, it fails when it reaches
You're passing in a string, and then Magento tries to call
getId
on the string. That's why you get an error.Magento expects you to pass in a category object, and not a category ID. Give this a try
addCategoryFilter()
的参数必须是一个对象类型为Mage_Catalog_Model_Category
。The parameter for
addCategoryFilter()
must be an object of typeMage_Catalog_Model_Category
.