Magento 1.9 Error – Fix Call to a Member Function Load on a Non-Object

magento-1.9module

I'm trying code like tutorial

http://devdocs.magento.com/guides/m1x/magefordev/mage-for-dev-5.html
But i can't get the same result . Please tell me why my code doesn't work

My error:

Fatal error: Call to a member function load() on a non-object in C:\xampp\htdocs\magento\app\code\core\Mage\Core\Model\Abstract.php on line 225

config.xml

<?xml version="1.0"?>
<config>
    <frontend>
        <routers>
            <weblog>
                <use>standard</use>
                <args>
                    <module>Magentotutorial_Weblog</module>
                    <frontName>weblog</frontName>
                </args>
            </weblog>
        </routers>
    </frontend>
    <global>
        <models>
            <weblog>
                <class>Magentotutorial_Weblog_Model</class>
            </weblog>
            <weblog_resource>
                <class>Magentotutorial_Weblog_Model_Resource</class>
                <entities>
                    <blogpost>
                        <table>blog_posts</table>
                    </blogpost>
                </entities>
            </weblog_resource>
        </models>
    </global>
</config>

IndexController

<?php
class Magentotutorial_Weblog_IndexController extends  Mage_Core_Controller_Front_Action {
    public function testModelAction() {
        $params = $this->getRequest()->getParams();
        $blogpost = Mage::getModel('weblog/blogpost');
        echo get_class($blogpost);
        //var_dump($blogpost);
        //echo("Loading the blogpost with an ID of ".$params['id']);
        $blogpost->load($params['id']);
        $data = $blogpost->getData();
        var_dump($data);
    }
}

Blogpost.php

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

Best Answer

When you get a model like in this line:

$blogpost = Mage::getModel('weblog/blogpost');

You can not just assume that this works. You have to first of all check the post-condition of that call:

$blogpost = Mage::getModel('weblog/blogpost');
if (!$blogpost) {
    throw new UnexpectedValueException('Expected Model not available.');
}

This will prevent that you run into the error you have as well. It should also give you a hint what has not been properly configured: The model you tried to get.

The solution then is to learn with a step debugger where the configuration for that model is normally taken from.

For example you could have a configuration cache enabled and the system is still running on older configuration where the model didn't exist. Or your configuration is up to date but the model was just not properly configured in it.


Next to verifying post-conditions you also need to verify pre-conditions in your case. I mixed the place where the error occured a bit, but thanks to your comment, a pre-condition test similar to the post-condition check I already outlined can be introduced in the load method, see the following example:

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

    /**
     * Load object data
     *
     * @param   integer $id
     * @return  Mage_Core_Model_Abstract
     */
    public function load($id, $field=null)
    {
        $resource =  $this->_getResource();
        if (!$resource) {
            throw new UnexpectedValueException('Resource instance is not available');
        }

        return parent::load($id, $field);
    }
}

This in your concrete case does effectively prevent the error and will show you which model is missing. The rest I already answered exactly applies here. As this is resource related you could already assume that this is about the configuration of your resource model. But the better lesson to learn is to step through with a step debugger as the parsing of config files then will reveal which information is missing and more importantly why.