views:

49

answers:

3

I was following through on Alan Storm's tutorial on Magento's Model and ORM basics and I've run into a bit of a problem. When I get to the portion where I load from the Model for the first time I get this error "Fatal error: Call to a member function load() on a non-object...". I've reset everything already and tried again from scratch but I still get the same problem. My code looks like this:

 $params = $this->getRequest()->getParams();
 $blogpost = Mage::getModel('weblog/blogpost');
 var_dump($blogpost);
 echo("Loading the blogpost with an ID of ".$params['id']);
 $blogpost->load($params['id']);

As you can see I dumped the contents of $blogpost and it shows that it is just a boolean false. My guess is that there's either a problem with the connection to the database or, for some reason, the code for Mage::getModel() didn't get installed correctly.

EDIT - Adding Code

There's so many that I just decided to pastebin them lol

app/code/local/Ahathaway/Weblog/controllers/IndexController.php

app/code/local/Ahathaway/Weblog/etc/config.xml

app/code/local/Ahathaway/Weblog/Model/Blogpost.php

app/etc/modules/Ahathaway_Weblog.xml

+1  A: 

My guess is that Mage cannot find your model class. Double check the module/model name and also verify if the model is in a correct place in the filesystem (it should be in app/code/local/Weblog/Model/Blogpost.php).

You also need to check if your config.xml correctly defines your model classes. It would be best if you could past your config.xml and your model class...

silvo
I added the code to the post. In your comment you said the path should be app/code/local/Weblog/Model/Blogpost.php, shouldn't it also have the namespace like mine does?
Aaron Hathaway
Of course it should, my bad :)
silvo
+2  A: 

Your Model/Blogpost.php file should actually be Model/Mysql4/Blogpost.php, and you are missing the real Model/Blogpost.php.

Anders Rasmussen
I apologize, what do you mean the real Model/Blogpost.php? Is there one that's included in the Magento package?
Aaron Hathaway
Take a look at Alan Storm's tutorial. There are two different files called Blogpost.php:The model: Model/Blogpost.phpThe resource model: Model/Mysql4/Blogpost.phpYou have:Resource model in wrong place: Model/Blogpost.php
Anders Rasmussen
+1  A: 

A quick glance reveals you're missing the model resource. Go back to the section around the following code example

File: app/code/local/Alanstormdotcom/Weblog/Model/Mysql4/Blogpost.php

class Alanstormdotcom_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract{
    protected function _construct()
    {
        $this->_init('weblog/blogpost', 'blogpost_id');
    }   
}
Alan Storm
Perfect, thank you! For whatever reason I got confused about the resource model deal haha.
Aaron Hathaway
"Models" give you the logical model ("get the email address, get the title, etc.") Resource Models do the actual querying to the database. The theory is you can drop in a new resource model that readys from another database version/package, without having to change your base model class.
Alan Storm