2

This Question is asked many times but still it's unresolved with definite solution. I am following this tutorial to implement the module http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html.

I am getting this error

Fatal error: Call to a member function load() on a non-object in C:\wamp\www\magentoapp\app\code\local\Mudit\Weblog\controllers\IndexController.php on line 9

while trying to fetch the data from the database table in the IndexController.php while others get this error in core files of magento abstract.php

Please, check the code for the config.xml, Blogpost.php and IndexController.php.

Please, help I am stuck with this from last 2 hours.

etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!--
    The module's node contains basic
    information about each Magento module
-->
<modules>

    <!--
        This must exactly match the namespace and module's folder
        names, with directory separators replaced by underscores
    -->
    <Mudit_Weblog>

        <!-- The version of our module, starting at 0.0.1 -->
        <version>0.0.1</version>

    </Mudit_Weblog>

</modules>

<frontend>
    <routers>
        <weblog>
            <use>standard</use>
            <args>
                <module>Mudit_Weblog</module>
                <frontName>weblog</frontName>
            </args>
        </weblog>
    </routers>
</frontend>

<global>
    <models>

        <weblog>
            <class>Mudit_Weblog_Model</class>
            <!--
            need to create our own resource, can't just
            use core_resource
            -->
            <resourceModel>weblog_resource</resourceModel>
        </weblog>

        <weblog_resource>
            <class>Mudit_Weblog_Model_Resource</class>
            <entities>
                <blogpost>
                    <table>blog_posts</table>
                </blogpost>
            </entities>
        </weblog_resource>

    </models>
</global>

Model/Blogpost.php

class Mudit_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost');
}
}

Model/Resource/Blogpost.php

    <?php class Mudit_Weblog_Model_Resource_Blogpost extends   Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
    $this->_init('weblog/blogpost', 'blogpost_id');
    }
    }
    ?>

Controllers/IndexController.php

<?php class Mudit_Weblog_IndexController extends Mage_Core_Controller_Front_Action 
{

public function testModelAction() 
{
    $params = $this->getRequest()->getParams();
    $blogpost = Mage::getModel('weblog/blogpost');
    echo("Loading the blogpost with an ID of ".$params['id']);
    $blogpost->load($params['id']);
    $data = $blogpost->getData();
    var_dump($data);
}
}
?>
  • did you create Magentotutorial/Weblog/Model/Blogpost.php? – Qaisar Satti Aug 17 '16 at 11:04
  • My namespace is Mudit instead of Magentotutorial. You can see in code for the Blogspot.php I have created class Mudit_Weblog_Model_Resource_Blogpost and location of Blogpost.php is app/code/local/Mudit/Web‌​log/Model/Blogpost.ph‌​p – mudit mehrotra Aug 17 '16 at 11:06
  • class name represent the file location too. add the code kindly add you are good to go – Qaisar Satti Aug 17 '16 at 11:10
2

etc/modules/Mudit_Weblog.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Mudit_Weblog>
            <active>true</active>
            <codePool>local</codePool>
        </Mudit_Weblog>
    </modules>
</config>

Mudit/Weblog/controllers/IndexController.php

<?php 
class Mudit_Weblog_IndexController extends Mage_Core_Controller_Front_Action 
{
public function indexAction()
{
    echo "sdfds";
}
public function testModelAction() 
{
    $params = $this->getRequest()->getParams();
    $blogpost = Mage::getModel('weblog/blogpost');
            echo get_class($blogpost);

    echo("Loading the blogpost with an ID of ".$params['id']);
    $blogpost->load($params['id']);
    $data = $blogpost->getData();
    var_dump($data);
}
}
?>

Mudit/Web‌​log/Model/Blogpost.ph‌​p

class Mudit_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract
{
    public function _construct()
    {
        parent::_construct();
        $this->_init('weblog/blogpost');
    }
 }

Mudit/Web‌​log/Model/Resource/Blogpost.ph‌​p

<?php class Mudit_Weblog_Model_Resource_Blogpost extends   Mage_Core_Model_Resource_Db_Abstract
{
    public function _construct()
    {
    $this->_init('weblog/blogpost', 'blogpost_id');
    }
    }
    ?>

Mudit/Weblog/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <modules>
        <Mudit_Weblog>
            <version>0.1.0</version>
        </Mudit_Weblog>
    </modules>
    <global>

        <models>
            <weblog>
                <class>Mudit_Weblog_Model</class>
                <resourceModel>weblog_resource</resourceModel>
            </weblog>
            <weblog_resource>
            <class>Mudit_Weblog_Model_Resource</class>
            <entities>
                <blogpost>
                    <table>blog_posts</table>
                </blogpost>
            </entities>
        </weblog_resource>
        </models>
    </global>

    <frontend>
        <routers>
            <weblog>
                <use>standard</use>
                <args>
                    <module>Mudit_Weblog</module>
                    <frontName>weblog</frontName>
                </args>
            </weblog>
        </routers>
    </frontend>

</config>
  • No it there in the code – mudit mehrotra Aug 17 '16 at 11:11
  • @muditmehrotra are the classes are same too? – Qaisar Satti Aug 17 '16 at 11:12
  • Blogpost.php is already in the Model folder. I have edited my question and made changes in the file paths – mudit mehrotra Aug 17 '16 at 11:13
  • i know it is there but the you added the class Mudit_Weblog_Model_Resource_Blogpost that is looking for Mudit/Weblog/Model/Resource/Blogpost.php so follow this answer and add these file with this code then tell me output – Qaisar Satti Aug 17 '16 at 11:15
  • Should I move the Blogpost.php file from Model/ to Model/Resource/ ? – mudit mehrotra Aug 17 '16 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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