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, $
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.
Order Collection Limit :
$orderCollection = Mage::getResourceModel('sales/order_collection');
$orderCollection->getSelect()->limit(10);
foreach ($orderCollection->getItems() as $order) :
$orderModel = Mage::getModel('sales/order');
$order = $orderModel->load($order['entity_id']);
echo $order->getId().'<br>';
endforeach;
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.
You can Implement this also:- setPage(1, n); where, n = any number.
$products = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToSelect(array('name', 'price', 'small_image'))
->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //visible only catalog & searchable product
->addAttributeToFilter('status', 1) // enabled
->setStoreId($storeId)
->setOrder('created_at', 'desc')
->setPage(1, 6);