2

I've created my Admin Page Grid but there is a strange issue when I try to prepare the collection. This is the code:

  protected function _prepareCollection()
  {
      $collection = Mage::getModel('catalog/product')->getCollection()
        ->addAttributeToSelect('*')
        ->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
        ->addAttributeToSort('entity_id', 'desc');
      $collection->getSelect()->limit(5,1);
      $this->setCollection($collection);
      return parent::_prepareCollection();
  }

and this the result:

the result

I've try to limit the result to 5 items but it is ignored. The system gets 740 items. The query generated seems to be right:

SELECT `e`.*, `at_visibility`.`value` AS `visibility` FROM `catalog_product_entity` AS `e` INNER JOIN `catalog_product_entity_int` AS `at_visibility` ON (`at_visibility`.`entity_id` = `e`.`entity_id`) AND (`at_visibility`.`attribute_id` = '102') AND (`at_visibility`.`store_id` = 0) WHERE (at_visibility.value = '4') ORDER BY `e`.`entity_id` desc LIMIT 5 OFFSET 1

I've covered the if the method return $this instead of parent::_prepareCollection(), I obtain the right items but in the Pager area I continue to see "Total 740 records found".

Where am I wrong?

2

You can modify a function called _preparePage in your grid class.

e.g.

protected function _preparePage()
{
    $this->getCollection()->setPageSize(5);
    $this->getCollection()->setCurPage(1);
}

Note: Its not a complete solution, just an example to show the usage.

Also you have to edit the app/design/adminhtml/default/default/template/widget/grid.phtml file to include the new page size in the grid pagination toolbar. (See this)

1
  • I've tried to use setDefaultLimit() and setDefaultPage() methods but they limit the page visibility.. I need to limit the query result. – WaPoNe Apr 17 '15 at 14:15
0

I'm not sure, but I think it's impossible to apply limit method to collection. I assume that magento add it's own limitation "view N items per page"

1
  • Yes, it is possible to apply limit method to collection. If my _prepareCollection() function returns $this instead of parent::_prepareCollection(), I obtain the right number of items. – WaPoNe Apr 17 '15 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.