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 SELECT
main_table.* FROM
promo_code_detailsAS
main_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
Best Answer
you can add limit
$this->setDefaultLimit(200)
for grid.