0

I want to link the model with the database in magento and get the data from the database but i can not figured it out. Is anybody help me to solve this problem. I spend long time to find its problem but i am fail. so i am here to find its solution, is there any Magento Geek who can help me to solve this problem? Its been very grateful if you can solve this problem.

I got following error when i run the below link: http://localhost/magentonew/weblog/index/testModel/id/1

Loading the blogpost with an ID of 1
Fatal error: Call to a member function load() on a non-object in C:\xampp\htdocs\magentonew\app\code\core\Mage\Core\Model\Abstract.php

on line 225

This is my code: ..\Alanstormdotcom\Weblog\etc\config.xml

    <?xml version="1.0" encoding="UTF-8"?>
<config>    
    <modules>
        <Alanstormdotcom_Weblog>
            <version>0.1.0</version>
        </Alanstormdotcom_Weblog>
    </modules>
    <frontend>
    <routers>
        <weblog>
            <use>standard</use>
            <args>
                <module>Alanstormdotcom_Weblog</module>
                <frontName>weblog</frontName>
            </args>
        </weblog>
    </routers>
</frontend>    

    <global>
    <!-- ... -->
    <models>
        <weblog>
            <class>Alanstormdotcom_Weblog_Model</class>
            <!--
            need to create our own resource, can't just
            use core_mysql4
            -->
            <resourceModel>weblog_mysql4</resourceModel>
             <weblog_mysql4>
            <class>Alanstormdotcom_Weblog_Model_Mysql4</class>
        </weblog_mysql4>

        </weblog>
    </models>
       <!-- ... -->
    <resources>
        <weblog_write>
            <connection>
                <use>core_write</use>
            </connection>
        </weblog_write>
        <weblog_read>
            <connection>
                <use>core_read</use>
            </connection>
        </weblog_read>
    </resources>
    <!-- ... -->
    </global>
</config>   

Contoller file:..\Alanstormdotcom\Weblog\controllers\IndexController.php

<?php

class Alanstormdotcom_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);
    }
}

Blogspot.php: ..\Alanstormdotcom\Weblog\Model\Blogspot.php

<?php 

class Alanstormdotcom_Weblog_Model_Blogpost extends Mage_Core_Model_Abstract

{

    protected function _construct()

    {

        $this->_init('weblog/blogpost');

    }

}

:..\Alanstormdotcom\Weblog\Model\Mysql4\Blogspot.php

<?php 


class Alanstormdotcom_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract

{

    protected function _construct()

    {

        $this->_init('alanstormdotcom_weblog/blogpost', 'blogpost_id');

    }   

}

and i also create table in my database using following query:

>     CREATE TABLE `blog_posts` (
>       `blogpost_id` int(11) NOT NULL auto_increment,
>       `title` text,
>       `post` text,
>       `date` datetime default NULL,
>       `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
>       PRIMARY KEY  (`blogpost_id`)
>     )
> INSERT INTO `blog_posts` VALUES (1,'My New Title','This is a blog
> post','2009-07-01 00:00:00','2009-07-02 23:12:30');
1

It looks like you're missing the resource model entity node, which associates your resource model to your custom table.

<models>
    <weblog>
        <class>Alanstormdotcom_Weblog_Model</class>
        <resourceModel>weblog_mysql4</resourceModel>
        <weblog_mysql4>
            <class>Alanstormdotcom_Weblog_Model_Mysql4</class>
            <entities>
                <blogpost>blog_posts</blogpost>
            </entities>
        </weblog_mysql4>
    </weblog>
</models>

Also

class Alanstormdotcom_Weblog_Model_Mysql4_Blogpost extends Mage_Core_Model_Mysql4_Abstract
{
    protected function _construct()
    {
        $this->_init('weblog/blogpost', 'blogpost_id');
    }   
}
| improve this answer | |
  • I update entities tag in my config.xml....However I got the same error. – Yogesh Chalodiya Apr 8 '15 at 12:27

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.