Magento - addCategoryFilter 抛出错误

发布于 2024-10-18 02:46:58 字数 755 浏览 3 评论 0原文

我成功运行了此查询:

 $_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天冷不及心凉 2024-10-25 02:46:58

这涵盖了 1.4.2。根据您的错误,我认为您运行的版本稍旧,但错误的根本原因应该是相同的。

类别名 reports/product_collection 解析为

Mage_Reports_Model_Mysql4_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 看起来像这样

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;
}

注意,在 1.4.2 中,参数中进行了一些类型检查,

public function addCategoryFilter(Mage_Catalog_Model_Category $category)

我怀疑您的版本没有这种检查。因此,当到达

$this->_productLimitationFilters['category_id'] = $category->getId();

You're pass in a string 时,它会失败,然后 Magento 尝试在该字符串上调用 getId 。这就是你收到错误的原因。

Magento 希望您传入类别对象,而不是类别 ID。尝试一下

$category = Mage::getModel('catalog/category')->load(3);
$p = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->setOrder('created_at', 'desc')
            ->addCategoryFilter($category)
            ->setPage(1, 5);

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);
梦巷 2024-10-25 02:46:58

addCategoryFilter() 的参数必须是一个对象类型为Mage_Catalog_Model_Category

The parameter for addCategoryFilter() must be an object of type Mage_Catalog_Model_Category.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文