Magento Admin – Problem with Loading Grid in the Admin Area

adminhtmlce-1.8.1.0configurationgridgrid-serlization

Folder Structure:

Companyname/Products/Block/Adminhtml/Products.php

class Companyname_Products_Block_Adminhtml_Products extends Mage_Adminhtml_Block_Widget_Grid_Container {

    public function __construct() {
        $this->_controller = 'adminhtml_products';
        $this->_blockGroup = 'companyname_products';
        $this->_headerText = Mage::helper('companyname_products')->__('Books Manager');
        $this->_addButtonLabel = Mage::helper('companyname_products')->__('Add Book');

        parent::__construct();
    }

}

Companyname/Products/Block/AdminHtml/Products/Grid.php

class Companyname_Products_Block_Adminhtml_Products_Grid extends Mage_Adminhtml_Block_Widget_Grid {

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

    protected function _prepareCollection() {
        $collection = Mage::getModel('companyname_products/products')->getCollection();

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns() {



        $this->addColumn('product_id', array(
            'header' => Mage::helper('companyname_products')->__('ID'),
            'align' => 'left',
            'width' => '10px',
            'index' => 'product_id',

        ));

        $this->addColumn('updated_at', array(
            'header' => Mage::helper('companyname_products')->__('Updated At'),
            'align' => 'left',
            'index' => 'updated_at',
            'width' => '20px',

        ));


        $this->addColumn('format', array(
            'header' => Mage::helper('companyname_products')->__('Format'),
            'width' => '10px',
            'index' => 'format',

        ));


        $this->addColumn('sku', array(
            'header' => Mage::helper('companyname_products')->__('Sku'),
            'width' => '30px',
            'index' => 'sku',

        ));

        $this->addExportType('*/*/exportCsv', Mage::helper('companyname_products')->__('CSV'));

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction() {

    }

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

}

etc/config.xml

<config>
    <modules>
        <companyname_Products>
            <version>0.1.0</version>
        </companyname_Products>
    </modules>
    <global>
        <helpers>
            <companyname_products>
                <class>companyname_Products_Helper</class>
            </companyname_products>    
        </helpers>
        <blocks>
            <companyname_products>
                <class>companyname_Products_Block</class>
            </companyname_products>
        </blocks>
        <models>
            <companyname_products>
                <class>companyname_Products_Model</class>
                <resourceModel>companyname_products_mysql4</resourceModel>
            </companyname_products>
            <companyname_products_mysql4>
                <class>companyname_Products_Mysql4</class>
                <entities>
                    <books>
                        <table>companyname_products_books</table>
                    </books>
                </entities>
            </companyname_products_mysql4>   
        </models>
        <resources>
            <companyname_products_setup>
                <setup>
                    <module>companyname_Products</module>
                    <class>companyname_Products_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </companyname_products_setup>
            <companyname_products_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </companyname_products_write>
            <companyname_products_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </companyname_products_read>
        </resources>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <companyname_products before="Mage_Adminhtml">companyname_Products_Adminhtml</companyname_products>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
    <adminhtml>
        <layout>
            <updates>
                <companyname_products module="companyname_products">
                    <file>companyname_products.xml</file>
                </companyname_products>
            </updates>
        </layout>


    </adminhtml>
</config>

etc/adminhtml.xml

<config>
    <menu>
        <companyname_products translate="title" module="companyname_products">
            <title>Upload Books</title>
            <sort_order>71</sort_order>
            <children>
                <items module="companyname_products">
                    <title>Manage Books</title>
                    <sort_order>0</sort_order>
                    <action>adminhtml/books/index</action>
                </items>
            </children>
        </companyname_products>
    </menu>
    <acl>
        <resources>
            <admin>
                <children>
                    <companyname_products translate="title" module="companyname_products">
                        <title>Upload Books</title>
                        <sort_order>300</sort_order>
                        <children>
                            <items translate="title" module="companyname_products">
                                <title>Manage Books</title>
                                <sort_order>0</sort_order>
                            </items>
                        </children>
                    </companyname_products>
                </children>
            </admin>
        </resources>
    </acl>
</config>

If you see errors in the xml files that has to do with uppercase letters ignore them.They are correct in the original file.

My problem and my question is:
The link Upload Books is there.The acl permissions are working fine.The database is created correctly.The grid container is created BUT not the grid itself. I get a page with the Add Book button and Manage Books label and not the grid.I also can echo through the function _prepareColumns() text..And i checked and the grid.phtml is loading and my Block.
I use v1.8.1 magento on Xampp.
Thanks a lot!

========================= EDIT ===========================

After many hours i realized that the problem is coming from the database.
if i do that:

 protected function _prepareCollection() {
        $collection = Mage::getModel('companyname_books/books')->getCollection();
            var_dump($collection);

        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

The result is bool(false). But from the way I am making a database I can't really find a problem.
Here is how I am doing them.

Model/Products.php

class Companyname_Products_Model_Books extends Mage_Core_Model_Abstract
{
    public function __construct() {
        $this->_init('comapnyname_products/books');
        parent::_construct();
    } 

}

Model/Mysql4/Products.php

class Companyname_Books_Model_Mysql4_Products extends Mage_Core_Model_Mysql4_Abstract {

    public function _construct() {
        $this->_init('comapnyname_products/books', 'book_id');
    }

}

Collection.php

class Companyname_Books_Model_Mysql4_Products_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {

    public function _construct() {
        $this->_init('comapnyname_products/books');
        parent::_construct();
    }

}

Best Answer

I think this is preventing it from working.
In the config.xml you declared your resource node like this:

<companyname_products_mysql4>
    <class>companyname_Products_Mysql4</class>
    <entities>
        <books>
            <table>companyname_products_books</table>
        </books>
    </entities>
</companyname_products_mysql4> 

The <class> tag is wrong. It should be

<class>Companyname_Products_Model_Mysql4</class>
Related Topic