【问题标题】:A model's collection turns out to be a non-object模型的集合结果是非对象
【发布时间】:2012-08-15 09:47:13
【问题描述】:

我正在尝试获取 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() 中返回的集合不是对象?

【问题讨论】:

  • 仅供参考:如果返回的对象不是集合,则会收到类似“调用非对象上的成员函数 addAttributeToFilter”的错误。您的异常正在发生,因为过滤器/集合代码中更深层次的东西遇到了问题。

标签: magento


【解决方案1】:

您的过滤器调用出错。实际上,产品没有 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 开发的乐趣!

【讨论】:

  • 我很困惑。我正在查看 report_viewed_product_index 表,它确实有 store_id(以及 visitor_id 和 customer_id 列)。我将如何根据所有这些列进行过滤?
  • 它按产品的商店关联过滤(catalog_category_product_index)
猜你喜欢
  • 2020-08-06
  • 1970-01-01
  • 2019-09-08
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多