0

我正在尝试在 Magento 中获取已查看产品的列表,但以下代码:

$model = Mage::getModel('reports/product_index_viewed')->getCollection()
    ->addAttributeToFilter('store_id', array('eq' => 1));

创建了错误消息:

致命错误:在第 816 行对 C:\xampp\htdocs\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php 中的非对象调用成员函数 getBackend()

为什么 getCollection() 中返回的集合是非对象?

4

1 回答 1

2

您的过滤器调用中有错误。实际上,产品没有 store_id 属性,在您的案例中,集合尝试获取此属性,但由于它不存在,因此会发生错误。在报告集合中,创建了一个特殊方法来指定存储过滤器,因此您的代码应该如下所示(我还包括了正确类型提示的构造):

/* @var $collection Mage_Reports_Model_Resource_Product_Viewed_Collection */ // This enabled type hinting
$collection = Mage::getModel('reports/product_index_viewed')->getCollection();
$collection->setStoreId($storeId); // Setting data scope (e.g translated names, prices, etc)
$collection->addStoreFilter($storeId); // Set filter by exact availability on this store.

享受 Magento 开发的乐趣!

于 2012-08-15T10:15:11.120 回答