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
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_collectionresolves toin resource mode context. The class
Mage_Reports_Model_Mysql4_Product_Collectionis a child ofMage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract. TheaddCategoryFilteronMage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstractlooks 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
getIdon 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