Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

php - Magento admin grid was not displaying only header title and add button are present

Thanks to @PHP Weblineindia for sharing a link for this tutorial http://navaneeth.me/creating-magento-extension-with-custom-database-table/#comment-8147.

I followed almost every details given by this tutorial however I can't display the table grid.

UPDATE: Upon Debugging my model collection has a problem

Another UPDATE: I update all the resource model and collection and extend them to this

Mage_Core_Model_Resource_Db_Abstract -> resource model

Mage_Core_Model_Resource_Db_Collection_Abstract -> Collection model

The good thing is that. I can now add new data to the database.

BUT the main problem still the same, no grid displayed.

I've tried simple data retrieve in magento front end but it give me Fatal error: Call to a member function load() on a non-object.

I'm still new to magento and there are lot of mysteries that I need to discover.

MODEL

app/code/local/Rts/Pmadmin/Model/Pmadmin.php

class Rts_Pmadmin_Model_Pmadmin extends Mage_Core_Model_Abstract {
protected function _construct()
{
    $this->_init('pmadmin/pricematrix');
}
}

app/code/local/Rts/Pmadmin/Model/Mysql4/Resource/Pmadmin.php

class Rts_Pmadmin_Model_Mysql4_Resource_Pmadmin extends Mage_Core_Model_Resource_Db_Abstract {
protected function _construct()
{
    $this->_init('pmadmin/pmadmin', 'pmadmin_id');
}
}

app/code/local/Rts/Pmadmin/Model/Mysql4/Resource/Collection.php

class Rts_Pmadmin_Model_Mysql4_Resource_Collection extends Mage_Core_Model_Resource_Db_Collection_Abstract {
public function _construct()
{
    parent::_construct();
    $this->_init('pmadmin/pmadmin');
}
}

These are the codes.

UPDATED config.xml

<global>
    <helpers>
        <pmadmin>
            <class>Rts_Pmadmin_Helper</class>
        </pmadmin>
    </helpers>
    <blocks>
        <pmadmin>
            <class>Rts_Pmadmin_Block</class>
        </pmadmin>
    </blocks>
    <models>
        <pmadmin>
            <class>Rts_Pmadmin_Model</class>
            <resourceModel>pmadmin_resource</resourceModel>
        </pmadmin>
        <pmadmin_resource>
            <class>Rts_Pmadmin_Model_Resource</class>
            <entities>
                <pmadmin>
                    <table>pmadmin</table>
                </pmadmin>
            </entities>
        </pmadmin_resource>
    </models>
    <resources>
        <pmadmin_setup>
            <setup>
                <module>Rts_Pmadmin</module>
               <class>Rts_Pmadmin_Model_Mysql4_Resource_Mysql4_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </pmadmin_setup>
        <pmadmin_write>
            <connection>
                <use>core_write</use>
            </connection>
        </pmadmin_write>
        <pmadmin_read>
            <connection>
                <use>core_read</use>
            </connection>
        </pmadmin_read>
    </resources>
    </global>

UPDATE: Content block

class Rts_Pmadmin_Block_Adminhtml_Pmadmin_Grid extends Mage_Adminhtml_Block_Widget_Grid {

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

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

protected function _prepareColumns() {
    $this->addColumn('pricematrix_id', array(
        'header' => Mage::helper('pmadmin')->__('ID'),
        'align' => 'right',
        'width' => '10px',
        'index' => 'pricematrix_id',
    ));

    $this->addColumn('title', array(
        'header' => Mage::helper('pmadmin')->__('Title'),
        'align' => 'left',
        'index' => 'title',
        'width' => '50px',
    ));


    $this->addColumn('short_description', array(
        'header' => Mage::helper('pmadmin')->__('Description'),
        'width' => '150px',
        'index' => 'short_description',
    ));

    $this->addColumn('file_path', array(
        'header' => Mage::helper('pmadmin')->__('File Path'),
        'width' => '150px',
        'index' => 'file_path',
    ));


    $this->addColumn('customer_group', array(
        'header' => Mage::helper('pmadmin')->__('Customer Group'),
        'width' => '150px',
        'index' => 'customer_group',
    ));


    $this->addColumn('creation_time', array(
        'header' => Mage::helper('pmadmin')->__('Posted On'),
        'width' => '150px',
        'index' => 'creation_time',
    ));

    return parent::_prepareColumns();
}


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

public function getGridUrl()
{
  return $this->getUrl('*/*/grid', array('_current'=>true));
}
}

PROBLEM:

  1. No grid is displayed only the header title and the add button

  2. When click add button, the forms are displayed however I can't add new Item

  3. Model Collection return "bool(false)" after var dump

QUESTION:

Can you help me spot the problem?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is a problem with your Model folder structure. I'm not an expert on magento but I think you separate your resource model and collection model into separate folder.

Based on @Nicolas D answer, I also encounter this problem on magento 1.9.1. It also took me several days to find out the solution.

Yes, @Nicolas D is right, you have to put your resource folder on Mysql4 folder. However, under Mysql4 folder, you have to create a folder the same name as your module name and put your Collection.php model.

For example:

Instead of

Rts_Pmadmin_Model_Mysql4_Resource

Make it

Rts_Pmadmin_Model_Mysql4_Pmadmin

Also! Don't forget to update your config.xml and other file related to your changes.

<models>
    <pmadmin>
        <class>Rts_Pmadmin_Model</class>
        <resourceModel>pmadmin_resource</resourceModel>
    </pmadmin>
    <pmadmin_resource>
        <class>Rts_Pmadmin_Model_Mysql4</class>
        <entities>
            <pmadmin>
                <table>pmadmin</table>
            </pmadmin>
        </entities>
    </pmadmin_resource>
</models>
<resources>
    <pmadmin_setup>
        <setup>
            <module>Rts_Pmadmin</module>
           <class>Rts_Pmadmin_Model_Mysql4_Pmadmin_Mysql4_Setup</class>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </pmadmin_setup>
    <pmadmin_write>
        <connection>
            <use>core_write</use>
        </connection>
    </pmadmin_write>
    <pmadmin_read>
        <connection>
            <use>core_read</use>
        </connection>
    </pmadmin_read>
</resources>

I don't know if this is a magento bug but this is how we solved our problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...