0

I am using following code to get collections of my custom created module,

app/code/local/Promocode/Promogenerator/Block/Adminhtml/Promogenerator/Grid.php

<?php

class Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
            parent::__construct();
            $this->setId("promogeneratorGrid");
            $this->setDefaultSort("id");
            $this->setDefaultDir("DESC");
            $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection()
    {
            $collection = Mage::getModel("promogenerator/promogenerator")
                        ->getCollection()
                        ->addFieldToSelect('*')
                        ->addFieldToFilter('status', array('neq' => '3'));
                        //->setPageSize(20);
                        //->setPageSize(20)
                        //->setCurPage(1);*/
            $this->setCollection($collection);
            echo $collection->load()->getSelect();
            //die();
            //return parent::_prepareCollection();
            return $this;
    }

    public function customerCallback($collection){
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
    protected function _prepareColumns()
    {
            $this->addColumn("id", array(
                "header" => Mage::helper("promogenerator")->__("ID"),
                "align" =>"right",
                "width" => "50px",
                "type" => "number",
                "index" => "id",
            ));

            $this->addColumn("roll_id", array(
            "header" => Mage::helper("promogenerator")->__("Roll"),
            "index" => "roll_id",
            'type' => 'options',
            'options'=>Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid::getOptionArray5(),             
            ));
            $this->addColumn("promocode", array(
            "header" => Mage::helper("promogenerator")->__("Promocode"),
            "index" => "promocode",
            ));
            $this->addColumn("promo_id", array(
                "header" => Mage::helper("promogenerator")->__("User"),
                "index"  => array('promo_id','share_id'),
                "type"   => 'concat',
                "separator" => '--',
                "filter_index" => "CONCAT(promo_id, '--', share_id)",
            ));
            /*$this->addColumn("share",array(
                "header" => Mage::helper("promogenerator")->__("User"),
                "index"  => "shareinfo",
            ));*/
            $this->addColumn("provider_id", array(
            "header" => Mage::helper("promogenerator")->__("Referrer"),
            "index" => "provider_id",
            ));
            $this->addColumn('status', array(
                'header' => Mage::helper('promogenerator')->__('Status'),
                'index' => 'status',
                'type' => 'options',
                'options'=>Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid::getOptionArray4(),             
            ));
            /* $this->addColumn('actions', array(
                'header'    => Mage::helper('promogenerator')->__('Action'),
                'width'     => 20,
                'sortable'  => false,
                'filter'    => false,
                'renderer'  => Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid_Renderer_Action,
            ));*/


        $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV')); 
        $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel'));

            return parent::_prepareColumns();
    }

    public function getRowUrl($row)
    {
           return $this->getUrl("*/*/edit", array("id" => $row->getId()));
    }



    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('id');
        $this->getMassactionBlock()->setFormFieldName('ids');
        $this->getMassactionBlock()->setUseSelectAll(true);
        $this->getMassactionBlock()->addItem('remove_promogenerator', array(
                 'label'=> Mage::helper('promogenerator')->__('Disable'),
                 'url'  => $this->getUrl('*/adminhtml_promogenerator/massRemove'),
                 'confirm' => Mage::helper('promogenerator')->__('Are you sure?')
            ));
        $this->getMassactionBlock()->addItem('delete_promogenerator', array(
                 'label'=> Mage::helper('promogenerator')->__('Remove'),
                 'url'  => $this->getUrl('*/adminhtml_promogenerator/massDelete'),
                 'confirm' => Mage::helper('promogenerator')->__('Are you sure?')
            ));
        $this->getMassactionBlock()->addItem('enable_promocode', array(
                 'label'=> Mage::helper('promogenerator')->__('Active'),
                 'url'  => $this->getUrl('*/adminhtml_promogenerator/massEnable'),
                 'confirm' => Mage::helper('promogenerator')->__('Are you sure?')
            ));
        return $this;
    }

    static public function getOptionArray4()
    {
        $data_array=array(); 
        $data_array[0]='In Active';
        $data_array[1]='Active';
        $data_array[2]='Disabled';
        return($data_array);
    }
    static public function getValueArray4()
    {
        $data_array=array();
        foreach(Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid::getOptionArray4() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);        
        }
        return($data_array);

    }

    static public function getOptionArray5()
    {
        $data_array=array(); 
        $data_array[1]='Admin';
        $data_array[2]='User';
        return($data_array);
    }
    static public function getValueArray5()
    {
        $data_array=array();
        foreach(Promocode_Promogenerator_Block_Adminhtml_Promogenerator_Grid::getOptionArray5() as $k=>$v){
           $data_array[]=array('value'=>$k,'label'=>$v);        
        }
        return($data_array);

    }

}

The above code works perfectly well for smaller collections, but with large ones the script will eventually run out of memory and display the blank page error.

I used setPage(20) to limit the collections, but its not working.

protected function _prepareCollection()
        {
                $collection = Mage::getModel("promogenerator/promogenerator")
                            ->getCollection()
                            ->addFieldToSelect('*')
                            ->addFieldToFilter('status', array('neq' => '3'))
                            ->setPageSize(20);              
                $this->setCollection($collection);
                //echo $collection->load()->getSelect();
                //die();
                return parent::_prepareCollection();
        }

Collections.php:,

class Promocode_Promogenerator_Model_Mysql4_Promogenerator_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
    {

        public function _construct(){
            $this->_init("promogenerator/promogenerator");
        }



    }

I echo out mysql query for the above code, using $collection->load()->getSelect();, that prints SELECTmain_table.* FROMpromo_code_detailsASmain_tableWHERE (status != '3') LIMIT 20.

Also used walk() method, that throws the following error,Fatal error: Call to a member function setPageSize() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php on line 553

I don't know what I am missing,.. can someone point me, why the setPage() not working?..

Thanks

10
  • $collection->setPageSize(20)->setCurPage(1); add curpage too Mar 7 '16 at 6:42
  • Thanks for the reply @QaisarSatti.. I tried that too but no change It display all the records
    – Gayathri
    Mar 7 '16 at 6:48
  • did you extend this block with toolbar block? Mar 7 '16 at 6:53
  • Sorry, I couldn't understand. what toolbar block? @QaisarSatti
    – Gayathri
    Mar 7 '16 at 6:55
  • i mean you are showing the toolbar on that? Mar 7 '16 at 6:57
2

you can add limit $this->setDefaultLimit(200) for grid.

public function __construct()
    {
            parent::__construct();
            $this->setId("promogeneratorGrid");
            $this->setDefaultSort("id");
            $this->setDefaultDir("DESC");
            $this->setDefaultLimit(200);
            $this->setSaveParametersInSession(true);
    }
7
  • Thanks for the answer @QaiserSatti. I have updated my code as your answer. But no changes at all.. It load the full database table on the grid.
    – Gayathri
    Mar 7 '16 at 7:32
  • make all changes to before remove the limit from collection also return $this; change to return parent::_prepareCollection(); Mar 7 '16 at 7:34
  • yes I removed the changes , done before. But Its not working.
    – Gayathri
    Mar 7 '16 at 7:40
  • make code to work on default 20 limit of grid Mar 7 '16 at 7:41
  • I tried that too..
    – Gayathri
    Mar 7 '16 at 7:54
1

May be try like this ,

$collection->getSelect()->limit(20);
2
  • Check if your model collection id extending from Mage_Core_Model_Mysql4_Collection_Abstract class Mar 7 '16 at 6:35
  • yes the model collection id is extending collection abstract class, also updated model collection.php at my question
    – Gayathri
    Mar 7 '16 at 6:42
1

The grid sets the page size in _preparePage() based on request parameters and default size, so changing it in _prepareCollection() does not have any effect.

protected function _preparePage()
{
    $this->getCollection()->setPageSize((int) $this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
    $this->getCollection()->setCurPage((int) $this->getParam($this->getVarNamePage(), $this->_defaultPage));
}

See Mage_Adminhtml_Block_Widget_Grid

So you either have to override this method or override the value of _defaultLimit

2
  • Thanks for the answer, I understood your explanation. But I want to select only 20 records from the table for this module only. How to achieve it?
    – Gayathri
    Mar 7 '16 at 8:06
  • As I said, override _preparePage(): $this->getCollection()->setPageSize(20); $this->getCollection()->setCurPage(1) Mar 7 '16 at 8:09

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.