5
votes

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

3

3 Answers

1
votes

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());
1
votes

Looks like you have the grid blocks set up correctly. However, you still need to load the grid into the layout and render it. This can either be done in the adminhtml layout xml or in the controller.

In your /app/design/adminhtml/../layout/brands.xml:

<?xml version="1.0"?>    
<layout>
        <brands_index_index>
            <reference name="content">
                <block type="brands/brands_grid" name="brands_grid"></block>
            </reference>
        </brands_index_index>
</layout>

In your controller:

public function indexAction()
{
    $this->loadLayout();
    $this->_addContent(
        $this->getLayout()->createBlock('brands/brands_grid','brands')
    );
    $this->renderLayout();
}

Please note that you have to modify the above to your particular implementation. I think the layout xml is harder to comprehend initially than the programmatic instantiation in the controller, however, in the long run, it leads to less code bloat.

0
votes

Just had a quick view and the only thing I can see in your code is:

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

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