0

I have created a custom module to show the data in admin grid

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('test_testing/order_collection');
    $collection->setProductFilter($this->_product->getId())->setParentItemIdFilter();
    $coreResource = Mage::getSingleton('core/resource');
    $collection->getSelect()
               ->joinLeft(array('billing_address' => $coreResource->getTableName('sales_flat_order_address')),
                    '`sales_order`.`billing_address_id` = `billing_address`.`entity_id`',
                    array('customer_firstname'  =>  'billing_address.firstname', 
                        'customer_lastname'     =>  'billing_address.lastname',
                        'customer_email'        =>  'billing_address.email',
                    ));

    $collection->getSelect()->limit(2);      
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Instead of 2 produts, It return's 4 products in admin grid.

I tried to print the query echo $collection->getSelect()->__toString(); and if i run the sql query it returns the correct record (i.e. 2)

I tried with ->setPage(1, 1); but still unable to resolve this issue.

It's Simple thing but still don't know what is missing. Please help.

8
  • Just a try... Have you tried $collection->setPageSize(2) instead of $collection->getSelect()->limit(2) ? Jan 24, 2019 at 12:06
  • yes, it return all 4 records Jan 24, 2019 at 12:07
  • its basic thing, i dun know why sometime simple thing give problem in magento :( Jan 24, 2019 at 12:10
  • Admin grid collections are normally affected by its toolbar / pager... so it's not as basic as it seems... Maybe you can play with _preparePage() method in your class, and try to force that parameters, although I think that would break user input in toolbar / pager. Take a look at Mage_Adminhtml_Block_Widget_Grid class Jan 24, 2019 at 12:14
  • yes, i tried with _preparePage() method protected function _preparePage() { $this->getCollection()->setPageSize(1); $this->getCollection()->setCurPage(1); } Jan 24, 2019 at 12:16

1 Answer 1

1

I think problem here is the logic having place when calling

return parent::_prepareCollection();

Which sets all the paging thing & load the collection. Just a try, not tested in this scenario

protected function _prepareCollection()
{
    $collection = Mage::getResourceModel('test_testing/order_collection');
    $collection->setProductFilter($this->_product->getId())->setParentItemIdFilter();
    $coreResource = Mage::getSingleton('core/resource');
    $collection->getSelect()
               ->joinLeft(array('billing_address' => $coreResource->getTableName('sales_flat_order_address')),
                    '`sales_order`.`billing_address_id` = `billing_address`.`entity_id`',
                    array('customer_firstname'  =>  'billing_address.firstname', 
                        'customer_lastname'     =>  'billing_address.lastname',
                        'customer_email'        =>  'billing_address.email',
                    ));

    $this->setCollection($collection);
    parent::_prepareCollection();

    $this->getCollection()->getSelect()->reset(Zend_Db_Select::LIMIT_COUNT);
    $this->getCollection()->getSelect()->reset(Zend_Db_Select::LIMIT_OFFSET);
    $this->getCollection()->clear()->setPageSize(2)->load();
    return $this;
}

If this works, I suppose it would break any input change by the user in Admin grid toolbar / pagination (but I understand it's precisely what you want to get)

4
  • still returning 4 products, 2 products in each page Jan 24, 2019 at 12:37
  • WHERE (main_table.product_id = '16') AND (ISNULL(parent_item_id) OR parent_item_id = '') ORDER BY qty_ordered DESC LIMIT 2 sql query is exactly correct, but how its overiding the sql query, very strange Jan 24, 2019 at 12:39
  • after few modification now its showing 2 products and only 1 page is displaying in pagination,which is correct,the only one problem is it show "Total 4 records found " :P Jan 25, 2019 at 5:40
  • Hi Raul, can you help me on this magento.stackexchange.com/questions/260088/… Feb 1, 2019 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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