1

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');
    }
}

2 Answers 2

1

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.

11
  • Thanks for your help but it the same error . And i make sure Model is available . Because when i echo get_class($blogpost) result is Magentotutorial_Weblog_Model_Blogpost . Please help me again . Thank you Commented Jul 26, 2015 at 10:23
  • @john: True, I haven't looked sharp enough on the error message. The model that is missing the load() method is the resource model the abstract model you extend from is missing. For that you have to check with getResourceModel() model first as you make indirect use of it.
    – hakre
    Commented Jul 26, 2015 at 10:27
  • I've updated the answer and added another example showing such a check. It was _getResource() and not getResourceModel but you could also check via Mage::getResourceModel() IIRC.
    – hakre
    Commented Jul 26, 2015 at 10:37
  • Thank you very much :D . But when i apply your code and reload the page i have new problem "Error log record number: 418625702" and default skin magento . Can you run code in part 5 tutorial after that tell me why my code is error . I have just learned it too difficult for me . Thank you . Commented Jul 26, 2015 at 10:45
  • Sure in that case the Exception you just put in the checks is getting thrown. The page you see is a hint you do not have development mode enabled otherwise you would have seen the exception message directly. See this existing Q&A material: Fundamentals for debugging a Magento store - Also keep in mind that you have to look with a step-debugger. You should only guess the first 1-3 minutes when debugging, after that, only follow hard facts, don't guess any longer.
    – hakre
    Commented Jul 26, 2015 at 10:51
1

I also struggled with the tutorial a lot but I have to say that it is a fantastic resource for Magento.

I think what you have missing is the following entry under your <weblog> section So,

<weblog>
  <class>Magentotutorial_Weblog_Model</class>
  <resourceModel>weblog_resource</resourceModel
</weblog>

Try this and let me know if it solves your problem (if it is still a relevant issue for you :))

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.