3 Comments

Magento Limiting Collections not working

giving 777 permissions for media folder
display shipping charges according to zip code in product view page

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:xampphtdocsmagentoappcodecoreMageAdminhtmlBlockWidgetGrid.php on line 553

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

Thanks

Related Posts

  • Adminhtml – Receiving error while trying to search for custom column in products grid (0)
    This is my adminhtml products grid.php: class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid { public function __construct() { parent::__construct(); $this->setId('productGrid'); […]
  • How To Override Custom Module Helper in Magento 2 (1)
    I would need to override the custom module helper into my custom module. Custom module code was, class Data extends AbstractHelper { const XML_PATH_BLOG = 'blog/'; protected $storeManager; protected $objectManager; protected $postfactory; protected […]
  • How To Override Custom Module Helper in Magento 2 (1)
    I would need to override the custom module helper into my custom module. Custom module code was, class Data extends AbstractHelper { const XML_PATH_BLOG = 'blog/'; protected $storeManager; protected $objectManager; protected $postfactory; protected […]
  • Recommended way to get the current categories product collection in Block (0)
    I tried something like this but all the data seems empty: <?php namespace VendorNameModuleNameBlockCategory; class Chooser extends MagentoFrameworkViewElementTemplate { protected $_coreRegistry = null; protected $_catalogLayer; protected $_categoryHelper; […]
  • Recommended way to get the current categories product collection in Block (0)
    I tried something like this but all the data seems empty: <?php namespace VendorNameModuleNameBlockCategory; class Chooser extends MagentoFrameworkViewElementTemplate { protected $_coreRegistry = null; protected $_catalogLayer; protected $_categoryHelper; […]
  • Recommended way to get the current categories product collection in Block (0)
    I tried something like this but all the data seems empty: <?php namespace VendorNameModuleNameBlockCategory; class Chooser extends MagentoFrameworkViewElementTemplate { protected $_coreRegistry = null; protected $_catalogLayer; protected $_categoryHelper; […]
  • Recommended way to get the current categories product collection in Block (0)
    I tried something like this but all the data seems empty: <?php namespace VendorNameModuleNameBlockCategory; class Chooser extends MagentoFrameworkViewElementTemplate { protected $_coreRegistry = null; protected $_catalogLayer; protected $_categoryHelper; […]
  • Orders Collection Magento 2 (1)
    my objective is to find all orders with status "pending". Seems the way to do this is to use the OrderCollection class Modelx extends MagentoFrameworkAppHelperAbstractHelper { /** * @var MagentoSalesModelResourceModelOrderCollectionFactory */ protected […]
  • Orders Collection Magento 2 (1)
    my objective is to find all orders with status "pending". Seems the way to do this is to use the OrderCollection class Modelx extends MagentoFrameworkAppHelperAbstractHelper { /** * @var MagentoSalesModelResourceModelOrderCollectionFactory */ protected […]
  • Extending Product View fails (1)
    I would like to extend MagentoCatalogBlockProductView but I get 404 <?php namespace VendorCustomBlock; use MagentoCatalogModelProductFactory; use MagentoCatalogBlockProductAbstractProduct; use MagentoFrameworkViewElementTemplate; use […]
giving 777 permissions for media folder
display shipping charges according to zip code in product view page

3 Comments on this article

  • User November 12, 2016

    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);
        }
    

    Reply

  • User November 12, 2016

    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

    Reply

  • User November 12, 2016

    May be try like this ,

    $collection->getSelect()->limit(20);
    

    Reply

Add a comment