-4

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

rodge
  • 264
  • 2
  • 4
  • 21
  • Way too much to read, especially after I saw you followed *almost* every detail.... – ElefantPhace Nov 17 '15 at 06:06
  • @ElefantPhace I appologize. – rodge Nov 17 '15 at 06:15
  • Just try to print collection in grid file is there you get collection or not. Go to Block->Adminhtml->Pmadmin->Grid.php protected function _prepareCollection() { $collection = Mage::getModel('pmadmin/pmadmin')->getCollection(); print_r($collection); die; $this->setCollection($collection); return parent::_prepareCollection(); } –  Nov 17 '15 at 09:31
  • I've tried this however it did'nt show anything. I updated also my models and I can now add new data to the database. – rodge Nov 18 '15 at 02:16
  • Hello Rodge, I think there is problem with your config.xml file follow all the steps as per given tutorial link. In your config.xml make and should be same as tutorial. I will suggest you to follow same copy as given link. –  Nov 18 '15 at 05:04
  • Hello @PHP Weblineindia, I updated my config.xml. The code is almost the same. On the frontend, tried this $test = Mage::getModel('pmadmin/pmadmin');var_dump($test->getData('title')); and returns NULL. – rodge Nov 18 '15 at 06:11
  • If you have a problem with your grid but the buttons are displayed, it means that you are loading the container but not the content block. Please provide blocks + controller – Nicolas D Nov 18 '15 at 11:53
  • @Nicolas D, I already updated and included the content grid. – rodge Nov 18 '15 at 12:13

2 Answers2

2

I had this problem once on my old magento version, which seems also to be your case, and the collection was false due to that :

You have to add Mysql4 in you classes names. Config:

<pmadmin_resource>
    <class>Rts_Pmadmin_Model_Mysql4_Resource</class>
    <entities>
        <pmadmin>
            <table>pmadmin</table>
        </pmadmin>
    </entities>
</pmadmin_resource>

Models:

Rts_Pmadmin_Model_Mysql4_Resource_Pricematrix_Collection
Rts_Pmadmin_Model_Mysql4_Resource_Pricematrix
Nicolas D
  • 1,182
  • 18
  • 40
  • yes exactly, my models fails to load data but it allow me to add new data on the database. Is it a magento bug? I updated the code however it didn't display the grid table. – rodge Nov 18 '15 at 12:57
  • did you updated well your model classes also on code? it is not updated here – Nicolas D Nov 18 '15 at 13:01
  • Sorry I forgot to update the post. I updated all my models and the config.xml but no luck – rodge Nov 18 '15 at 13:13
  • no errors in logs i guess? if not, try to debug one by one your widget block functions to see what is loaded and what is not – Nicolas D Nov 18 '15 at 13:22
  • Yes no error logs. I don't have the debugging skills in magento but I will try this technique from http://stackoverflow.com/questions/28320628/magento-widget-grid-only-fires-construct ---- and ------ http://stackoverflow.com/questions/4784117/magento-how-to-debug-blank-white-screen – rodge Nov 18 '15 at 13:38
  • basically, var_dump('my_function) in each function is enough sometimes to understand – Nicolas D Nov 18 '15 at 13:46
  • I tried to debug the grid content block and it turns out that the model collection is returning Null or Bool(false). – rodge Nov 19 '15 at 01:06
2

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.

Tyler
  • 156
  • 5