I am following this tutorial http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics.
As per this tutorial,when i pass a value with url like(http://localhost/dev/weblog/index/testModel/id/1), I am getting error like this
Fatal error: Call to a member function load() on a non-object in app\code\local\Srivats\Weblog\controlle开发者_Python百科rs\IndexController.php on line 10
This is my index controller code
<?php
class Srivats_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);
}
}
Blogpost.php file
<?php
class Srivats_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract
{
protected function _construct()
{
$this->_init('weblog/blogpost','blogpost_id');
}
}
Here is my config file
<?xml version="1.0" encoding="utf-8"?>
<config>
<global>
<modules>
<srivats_weblog>
<version>1.0.0</version>
</srivats_weblog>
</modules>
<models>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog_mysql4>
</models>
</global>
<frontend>
<routers>
<weblog>
<use>standard</use>
<args>
<module>Srivats_Weblog</module>
<frontName>weblog</frontName>
</args>
</weblog>
</routers>
</frontend>
</config>
system.log shows
Warning: include() [function.include]: Failed opening 'Mage\Weblog\Model\Blogpost.php' for inclusion (include_path='app\code\local;E:\dev\app\code\community;app\code\core;E:\dev\lib;.;C:\php\pear') in lib\Varien\Autoload.php on line 93
Here is the link to all files https://gist.github.com/cf99e4277599954f38d4
I think this line $blogpost->load($params['id']);
causing trouble.What i am missing.Any pointers?
Ahh in you config you should have
<global>
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
</model>
</global>
you already have for mysql4 resource add this on top of that. You should have
<models>
<weblog>
<class>Srivats_Weblog_Model</class>
<resourceModel>weblog_mysql4</resourceModel>
</weblog>
<weblog_mysql4>
<class>Srivats_Weblog_Model_Mysql4</class>
</weblog_mysql4>
</models>
Your real problem is that the line:
$blogpost = Mage::getModel('weblog/blogpost');
Is not returning an object and so $blogpost is just NULL.
Is the blogpost model class file actually created in app/code/local/Srivats/Weblog/Model/Blogpost.php
精彩评论