Magento: Set LIMIT on collection

后端 未结 4 1440
甜味超标
甜味超标 2021-02-01 01:58

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, $

相关标签:
4条回答
  • 2021-02-01 02:44

    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.

    0 讨论(0)
  • 2021-02-01 02:44

    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; 
    
    0 讨论(0)
  • 2021-02-01 02:45

    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.

    0 讨论(0)
  • 2021-02-01 02:49

    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);
    
    0 讨论(0)
提交回复
热议问题