如何限制Magento中的结果数? [英] How to limit the number of result within Magento?

查看:68
本文介绍了如何限制Magento中的结果数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在目录/产品控制器中有一个最近的产品操作.

I've a recent products action in my Catalog/Product controllers.

我检索全部产品(按实体ID排序).效果很好.

I retrieve all the product sorted by entity id. Works great ok.

问题是我只想在4页中显示20个产品.

The problem is I just want to show 20 products in 4 pages.

我尝试扩展Mage_Catalog_Block_Product_List并覆盖_getProductCollection(),我做了这样的事情:

I tried to extend Mage_Catalog_Block_Product_List and override _getProductCollection() and I did something like this:

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter();

重要的部分:

$this->_productCollection->getSelect()->limit($this->getProductsLimit());

如果我转储"返回的对象,我将具有以下内容:

If I 'dump' the returned Object I've something like this:

["limitcount"] => int(5) ["limitoffset"] => int(0)

["limitcount"] => int(5) ["limitoffset"] => int(0)

因此分页器似乎会将其覆盖.

So it looks to be overriden by the paginator.

您知道一种适当限制结果数量的方法吗?

Do you know a way to limit properly the number of result?

出于优化和展示的目的,我实际上不想检索所有产品集合.

For both optimization and presentation I actually don't want to retrieve all the products collection.

谢谢

推荐答案

应该有效...

$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter()
                ->setPage($pageNum, $pageSize);

// only retrieve 10 products
$this->_productCollection = Mage::getModel('catalog/product')->getCollection()
                ->addAttributeToSort('entity_id', 'desc')
                ->addStoreFilter()
                ->setPage(0, 10);

这篇关于如何限制Magento中的结果数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆