2

I created a custom table and would like to show the data from this table in a grid in admin section. I have created configuration for viewing the admin page. But there is an error while getting data from the table.

The error is :

Fatal error: Call to a member function getCollection() on a non-object in

My table name is "product_edit_details".

the config file is

    <?xml version="1.0"?>
    <config>
    <modules>
        <Buildnext_Producteditrequest>
            <version>0.1.0</version>
        </Buildnext_Producteditrequest>
    </modules>
    <frontend>
        <routers>
            <producteditrequest>
                <use>standard</use>
                <args>
                    <module>BuildnextProducteditrequest</module>
                    <frontName>producteditrequest</frontName>
                </args>
            </producteditrequest>
        </routers>
        <layout>
            <updates>
                <producteditrequest>
                    <file>producteditrequest.xml</file>
                </producteditrequest>
            </updates>
        </layout>
    </frontend> 
    <admin>
        <routers>
            <producteditrequest>
                <use>admin</use>
                <args>
                    <module>Buildnext_Producteditrequest</module>
                    <frontName>producteditrequest</frontName>
                </args>
            </producteditrequest>
        </routers>
    </admin> 
    <adminhtml>
        <menu>
            <producteditrequest module="producteditrequest">
                <title>Producteditrequest</title>
                <sort_order>71</sort_order>               
                <children>
                    <items module="producteditrequest">
                        <title>Manage Items</title>
                        <sort_order>0</sort_order>
                        <action>producteditrequest/adminhtml_producteditrequest</action>
                    </items>
                </children>
            </producteditrequest>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <producteditrequest>
                            <title>Producteditrequest Module</title>
                            <sort_order>200</sort_order>
                        </producteditrequest>
                    </children>
                </admin>
            </resources>   
        </acl>
        <layout>
            <updates>
                <producteditrequest>
                    <file>producteditrequest.xml</file>
                </producteditrequest>
            </updates>
        </layout>
    </adminhtml>  
    <global>
        <models>
            <producteditrequest>
                <class>Buildnext_producteditrequest_Model</class>
                <resourceModel>producteditrequest_mysql4</resourceModel>
            </producteditrequest>
            <producteditrequest_mysql4>
                <class>Buildnext_Producteditrequest_Model_Mysql4</class>
                <entities>
                    <producteditrequest>
                        <table>product_edit_details</table>
                    </producteditrequest>
                </entities>
            </producteditrequest_mysql4>
        </models>
        <resources>
            <producteditrequest_setup>
                <setup>
                    <module>Buildnext_Producteditrequest</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </producteditrequest_setup>
            <producteditrequest_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </producteditrequest_write>
            <producteditrequest_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </producteditrequest_read>
        </resources>
        <blocks>
            <producteditrequest>
                <class>Buildnext_Producteditrequest_Block</class>
            </producteditrequest>
        </blocks>
        <helpers>
            <producteditrequest>
                <class>Buildnext_Producteditrequest_Helper</class>
            </producteditrequest>
        </helpers>
    </global>
</config>

/Producteditrequest/Model/Producteditrequest.php

class Buildnext_Producteditrequest_Model_Producteditrequest extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('producteditrequest/producteditdetails');
    }
}

I have doubt regarding this line $this->_init('producteditrequest/producteditdetails'); -- My table name is product_edit_details tried giving $this->_init('producteditrequest/product_edit_details'); but was not working so i removed the underscores after reading this answer.

/Producteditrequest/Model/Mysql4/Producteditrequest.php

class Buildnext_Producteditrequest_Model__Mysql4_Producteditrequest 
      extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('producteditrequest/producteditdetails','pdt_edit_id');
    }
}

/Producteditrequest/Model/Mysql4/Producteditrequest/Collection.php

class Buildnext_Producteditrequest_Model_Mysql4_Producteditrequest_Collection 
      extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::__construct();
        $this->_init('producteditrequest/producteditdetails');
    }
}
11
  • Use producteditrequest in place of producteditdetails in init() function Feb 6, 2016 at 6:26
  • I have made that change in following locations but still getting that same error /Producteditrequest/Model/Producteditrequest.php,/Producteditrequest/Model/Mysql4/Producteditrequest.php,/Producteditrequest/Model/Mysql4/Producteditrequest/Collection.php. Could it be something with the table name ? Or the configuration? Feb 6, 2016 at 6:31
  • Compilation is enable? Did you flush cache? Feb 6, 2016 at 6:33
  • Yes i have flushed the cache... Not sure about what you meant by "Compilation" :) sorry about that.... Feb 6, 2016 at 6:38
  • Check System > tools > compilation . Disable it Feb 6, 2016 at 6:40

2 Answers 2

3

Follow bellow steps :

Step : 1 config.xml

 <?xml version="1.0"?>
    <config>
    <modules>
        <Buildnext_Producteditrequest>
            <version>0.1.0</version>
        </Buildnext_Producteditrequest>
    </modules>
    <frontend>
        <routers>
            <producteditrequest>
                <use>standard</use>
                <args>
                    <module>BuildnextProducteditrequest</module>
                    <frontName>producteditrequest</frontName>
                </args>
            </producteditrequest>
        </routers>
        <layout>
            <updates>
                <producteditrequest>
                    <file>producteditrequest.xml</file>
                </producteditrequest>
            </updates>
        </layout>
    </frontend> 
    <admin>
        <routers>
            <producteditrequest>
                <use>admin</use>
                <args>
                    <module>Buildnext_Producteditrequest</module>
                    <frontName>producteditrequest</frontName>
                </args>
            </producteditrequest>
        </routers>
    </admin> 
    <adminhtml>
        <menu>
            <producteditrequest module="producteditrequest">
                <title>Producteditrequest</title>
                <sort_order>71</sort_order>               
                <children>
                    <items module="producteditrequest">
                        <title>Manage Items</title>
                        <sort_order>0</sort_order>
                        <action>producteditrequest/adminhtml_producteditrequest</action>
                    </items>
                </children>
            </producteditrequest>
        </menu>
        <acl>
            <resources>
                <all>
                    <title>Allow Everything</title>
                </all>
                <admin>
                    <children>
                        <producteditrequest>
                            <title>Producteditrequest Module</title>
                            <sort_order>200</sort_order>
                        </producteditrequest>
                    </children>
                </admin>
            </resources>   
        </acl>
        <layout>
            <updates>
                <producteditrequest>
                    <file>producteditrequest.xml</file>
                </producteditrequest>
            </updates>
        </layout>
    </adminhtml>  
    <global>
        <models>
            <producteditrequest>
                <class>Buildnext_Producteditrequest_Model</class>
                <resourceModel>producteditrequest_mysql4</resourceModel>
            </producteditrequest>
            <producteditrequest_mysql4>
                <class>Buildnext_Producteditrequest_Model_Mysql4</class>
                <entities>        
                    <producteditrequest>
                        <table>product_edit_details</table>
                    </producteditrequest>
                </entities>
            </producteditrequest_mysql4>
        </models>
        <resources>
            <producteditrequest_setup>
                <setup>
                    <module>Buildnext_Producteditrequest</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </producteditrequest_setup>
            <producteditrequest_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </producteditrequest_write>
            <producteditrequest_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </producteditrequest_read>
        </resources>
        <blocks>
            <producteditrequest>
                <class>Buildnext_Producteditrequest_Block</class>
            </producteditrequest>
        </blocks>
        <helpers>
            <producteditrequest>
                <class>Buildnext_Producteditrequest_Helper</class>
            </producteditrequest>
        </helpers>
    </global>
</config>

Step : 2 /Buildnext/Producteditrequest/Model/Producteditrequest.php

class Buildnext_Producteditrequest_Model_Producteditrequest extends Mage_Core_Model_Abstract
{
    protected function _construct()
    {
        $this->_init("producteditrequest/producteditrequest");
    }
}

Step : 3 Buildnext/Producteditrequest/Model/Mysql4/Producteditrequest.php

class _Producteditrequest_Model_Mysql4_Producteditrequest extends Mage_Core_Model_Mysql4_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('producteditrequest/producteditrequest','pdt_edit_id');
    }
}

Step : 4 Buildnext/Producteditrequest/Model/Mysql4/Producteditrequest/Collection.php

class Buildnext_Producteditrequest_Model_Mysql4_Producteditrequest_Collection 
      extends Mage_Core_Model_Mysql4_Collection_Abstract
{
    public function _construct()
    {
        parent::__construct();
        $this->_init('producteditrequest/producteditrequest');
    }
}

Step : 5 Buildnext/Producteditrequest/Model/Mysql4/sql/producteditrequest_setup/mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();
$installer->run("
DROP TABLE IF EXISTS {$this->getTable('producteditrequest/producteditrequest')};
CREATE TABLE {$this->getTable('producteditrequest/producteditrequest')} (
  `pdt_edit_id` int(11) unsigned NOT NULL auto_increment,
  `created_at` datetime NULL,
  `updated_at` datetime NULL,
  PRIMARY KEY (`pdt_edit_id`)    
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");  

$installer->endSetup();

Step : 6 app\etc\modules\Buildnext_Producteditrequest.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Buildnext_Producteditrequest>
            <active>true</active>
            <codePool>local</codePool>
        </Buildnext_Producteditrequest>
    </modules>
</config> 

Step : 7 Refresh all magento Cache to admin side System -> Cache Management

1

For creating the custom model please create the model,resource and collection files inside the model folder.

enter image description here

Add the following code in the collection.php file.

/*
 app\code\local\Display\Customgrid\Model\Resource\Displaygrid\Collection.php
 */

 <?php

 class Display_Customgrid_Model_Resource_Displaygrid_Collection extends     Mage_Core_Model_Resource_Db_Collection_Abstract {
/**
 *
 */
protected function _construct()
{
    $this->_init('customgrid/displaygrid');
}
 }

Please click here for full code. http://www.pearlbells.co.uk/code-snippets/create-custom-model-magento/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.