Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid doesn't appear in custom admin module in Magento

Tags:

magento

I am trying to create a custom module in magento admin. I have reached the point where a new link has been added to the menu and by clicking on it, I can navigate to the index action of the controller of the module. But here I cannot see the grid, only the header text and the button which has been added in the block construct appear.

I can see that since this block extends the Mage_Adminhtml_Block_Widget_Grid_Container class, it will by itself add the grid block inside this module as its child.

And the Grid.php is included which I verified by printing out something in the overriden _prepareColumns method.

What am I missing here ?

These are the contents of the Grid.php file

class Book_Brands_Block_Adminhtml_Brands_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    public function __construct() {
        parent::__construct();
        $this->setId('brandsGrid');
        $this->setDefaultSort('brands_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }

    protected function _prepareCollection() {       
        $collection = Mage::getModel('brands/brands')->getCollection();
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns() {

        $this->addColumn('brands_id', array(
            'header' => Mage::helper('brands')->__('ID'),
            'align'  =>'right',
            'width'  => '50px',
            'index'  => 'brands_id',
        ));
        $this->addColumn('title', array(
            'header'=> Mage::helper('brands')->__('Title'),
            'align' =>'left',
            'index' => 'title',
        ));
        $this->addColumn('status', array(
            'header'=> Mage::helper('brands')->__('Status'),
            'align' => 'left',
            'width' => '80px',
            'index' => 'status',
            'type'  => 'options',
            'options' => array(
                1 => 'Enabled',
                2 => 'Disabled',
            ),
        ));
        $this->addColumn('action', array(
            'header' => Mage::helper('brands')->__('Action'),
            'width'  => '100',
            'type'   => 'action',
            'getter' => 'getId',
            'actions' => array(
                array(
                    'caption'  => Mage::helper('brands')->__('Edit'),
                    'url'  => array('base'=> '*/*/edit'),
                    'field' => 'id'
                )
            ),
            'filter'  => false,
            'sortable' => false,
            'index' => 'stores',
            'is_system' => true,
        ));
        return parent::_prepareColumns();
    }

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

Thanks

PS. I have tried flushing the cache but no luck

like image 826
naiquevin Avatar asked Nov 05 '22 00:11

naiquevin


1 Answers

From memory I think _prepareColumns() is called before _prepareCollection() so if there is an error in the collection the grid won't get rendered even though you have confirmed the columns method.

Part of parent::_prepareCollection() tries to estimate the number of pages from the collection's getSize() and getSelectCountSql() methods, I often forget to check those are producing sane results which trips me up. Make sure all logging is turned on and put the following in your .htaccess file:

php_flag display_errors on
SetEnv MAGE_IS_DEVELOPER_MODE true

Try seeing what query is being generated with these commands:

Mage::log((string)$collection->getSelect());
Mage::log((string)$collection->getSelectCountSql());
like image 91
clockworkgeek Avatar answered Dec 10 '22 19:12

clockworkgeek