Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: Set LIMIT on collection

The question that I tried to find out was how do we set a Limit on a Collection, the answers that I found on Google was only available for the Catalog with a setPage($pageNum, $pageSize). That didn't work on any other collections.
See the answer below.

like image 909
Shadowbob Avatar asked Jan 14 '13 16:01

Shadowbob


People also ask

How to set limit in collection in Magento 1. 9?

Here is the alternative and maybe more readable way: $collection = Mage::getModel('...')->getCollection(); $collection->getSelect()->limit(20); This will call Zend Db limit. You can set offset as second parameter.


2 Answers

There are several ways to do this:

$collection = Mage::getModel('...')             ->getCollection()             ->setPageSize(20)             ->setCurPage(1); 

Will get first 20 records.

Here is the alternative and maybe more readable way:

$collection = Mage::getModel('...')->getCollection(); $collection->getSelect()->limit(20); 

This will call Zend Db limit. You can set offset as second parameter.

like image 168
freento Avatar answered Sep 23 '22 01:09

freento


The way to do was looking at the code in code/core/Mage/Catalog/Model/Resource/Category/Flat/Collection.php at line 380 in Magento 1.7.2 on the function setPage($pageNum, $pageSize)

 $collection = Mage::getModel('model')      ->getCollection()      ->setCurPage(2) // 2nd page      ->setPageSize(10); // 10 elements per pages 

I hope this will help someone.

like image 26
Shadowbob Avatar answered Sep 21 '22 01:09

Shadowbob