1

Let's start with my adminhtml layout (where I think the issue is)

/app/design/adminhtml/default/default/layout/mattforms.xml

<?xml version="1.0"?>
<layout version="0.1.0">
<adminhtml_contacts_index>
    <reference name="content">
        <block name="grid" type="matt/adminhtml_contacts_grid"/>
    </reference>
</adminhtml_contacts_index>
</layout>

Block - /app/code/local/Shero/Matt/Block/Adminhtml/Contacts.php

<?php  
class Shero_Matt_Block_Adminhtml_Contacts extends Mage_Adminhtml_Block_Template 
{
    public function __construct() 
    {
        $this->_controller = 'adminhtml_contacts';
        $this->_blockGroup = 'matt';
        $this->_headerText = Mage::helper('Shero_Matt')->__('Contacts Manager');
        $this->_addButtonLabel = Mage::helper('Shero_Matt')->__('Add Contact');
        parent::_construct();
    }
}
?>

Grid Block - /app/code/local/Shero/Matt/Block/Adminhtml/Contacts/Grid.php

<?php
class Shero_Matt_Block_Adminhtml_Contacts_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
    public function __construct()
    {
        parent::_construct();
        $this->setId('contactsGrid');
        $this->setDefaultSort('id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
    }
    protected function _prepareCollection()
    {
        $collection = Mage::getModel('matt/contacts')->getCollection();  
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }
    protected function _prepareColumns()
    {
        $this->addColumn('id', array(
            'header' => Mage::helper('Shero_Matt')->__('ID'),
            'align'  => 'right',
            'width'  => '100px',
            'index'  => 'matt_contact_id',
        ));  
        $this->addColumn('firstName', array(
            'header' => Mage::helper('Shero_Matt')->__('Name'),
            'align'  => 'left',
            'width'  => '50px',
            'index'  => 'first_name',
        ));
        $this->addColumn('lastName', array(
             'header' => Mage::helper('Shero_Matt')->__('Last Name'),
            'align'  => 'left',
            'width'  => '50px',
            'index'  => 'last_name',
        ));
        return parent::_prepareColumns();
    }
}
?>

controller - /app/code/local/Shero/Matt/controllers/Adminhtml/ContactsController.php

<?php
class Shero_Matt_Adminhtml_ContactsController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
    public function gridAction()
    {
        $this->loadLayout();
        $this->renderLayout();
    }
}
?> 

adminhtml.xml - /app/code/local/Shero/Matt/etc/config.xml

<config>
    <menu>
        <Shero translate="title" module="Shero_Matt">
            <title>Contacts</title>
            <sort_order>1</sort_order>
            <children>
                <shero module="Shero_Matt">
                    <title>View Contacts</title>
                    <sort_order>1</sort_order>
                    <action>adminhtml/contacts/index</action>
                </shero>
             </children>
        </Shero>
    </menu>
</config>

config.xml /app/code/local/Shero/Matt/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Shero_Matt>
             <version>1.0.0</version>
        </Shero_Matt>
    </modules>
    <frontend>  
        <routers>
            <matt>
                <use>standard</use>
                <args>
                    <module>Shero_Matt</module>
                    <frontName>mattforms</frontName>
                </args>
            </matt>
        </routers>
        <events>
            <contact_data_saved>
                <observers>
                    <shero_matt>
                        <type>model</type>
                        <class>matt/observer</class>
                        <method>contactDataSaved</method>
                    </shero_matt>
                </observers>
            </contact_data_saved>
        </events>
        <layout>
            <updates>
                <matt>
                    <file>mattcontactform.xml</file>
                </matt>
            </updates>
        </layout>
    </frontend>
    <global>
        <blocks>
            <matt>
                <class>Shero_Matt_Block</class>
            </matt>
        </blocks>
        <models>
            <matt>
                <class>Shero_Matt_Model</class>
                <resourceModel>matt_mysql</resourceModel>
            </matt>
            <matt_mysql>
                <class>Shero_Matt_Model_Mysql</class>
                <entities>
                    <contacts>
                        <table>matt_contact</table>
                    </contacts>
                </entities>
            </matt_mysql>
        </models>
        <resources>
            <matt_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </matt_write>
            <matt_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </matt_read>
        </resources>
        <helpers>
            <Shero_Matt>
                <class>Shero_Matt_Helper</class>
            </Shero_Matt>
        </helpers>
        <template>
            <email>
                <matt_email_template translate="label" module="matt">
                    <label>Contacts Email</label>
                    <file>matt_email_template.html</file>
                    <type>html</type>
                </matt_email_template>
            </email>
        </template>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <Shero_Matt after="Mage_Adminhtml">Shero_Matt_Adminhtml</Shero_Matt> 
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <contacts>
                    <file>mattcontactform.xml</file>
                </contacts>
            </updates>
        </layout>
    </adminhtml>
</config>

What's important to note, if in Grid.php in my blocks, I change the a column from the database to be wrong, it throws the following:

'matt_contacts_id' in 'order clause', query was: SELECT `main_table`.* FROM `matt_contact` AS `main_table` ORDER BY matt_contacts_id ASC LIMIT 20

EDIT:

<block name="grid" type="matt/adminhtml_contacts_grid"/> 

should also be

<block name="grid" type="matt/adminhtml_contacts"/> 

in adminhtml layout for the grid container!

1 Answers1

0

Your class Shero_Matt_Block_Adminhtml_Contacts is a Grid Container. Grid containers need to extend the class Mage_Adminhtml_Block_Widget_Grid_Container. It is this class that calls _prepareLayout() which instantiates your grid class.

Your __construct() method inside this class also calls parent::_construct() (note the single underscore instead of two underscores). It needs to call parent::__construct().

I hope this helps.

Ryan Street
  • 2,020
  • 14
  • 12
  • THANK YOU! You have no idea how long I spent on this trying to figure out where I went wrong. I was also, from your answer able to check and see that in my grid.php I needed to use __construct instead of _construct. – Matthew Clayton Feb 07 '15 at 06:17