I'm successfully running this query:
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->set开发者_运维问答Order('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
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 to
Mage_Reports_Model_Mysql4_Product_Collection
in resource mode context. The class Mage_Reports_Model_Mysql4_Product_Collection
is a child of Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
. The addCategoryFilter
on Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract
looks like this
public function addCategoryFilter(Mage_Catalog_Model_Category $category)
{
$this->_productLimitationFilters['category_id'] = $category->getId();
if ($category->getIsAnchor()) {
unset($this->_productLimitationFilters['category_is_anchor']);
}
else {
$this->_productLimitationFilters['category_is_anchor'] = 1;
}
($this->getStoreId() == 0)? $this->_applyZeroStoreProductLimitations() : $this->_applyProductLimitations();
return $this;
}
Notice that in 1.4.2 there's some type checking going on in the paramaters
public function addCategoryFilter(Mage_Catalog_Model_Category $category)
I suspect your version doesn't have that checking. Therefore, it fails when it reaches
$this->_productLimitationFilters['category_id'] = $category->getId();
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
$category = Mage::getModel('catalog/category')->load(3);
$p = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setOrder('created_at', 'desc')
->addCategoryFilter($category)
->setPage(1, 5);
The parameter for addCategoryFilter()
must be an object of type Mage_Catalog_Model_Category
.
精彩评论