Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Basic Question throught Reading The Tutorial

Tags:

magento

I am new to Magento. Some questions make me crazy in the tutorial on their document. 1, How to open the development mode in magento 1.5.1?

2,"Fatal error: Call to a member function load() on a non-object in D:\PHPWeb\mag\app\code\local\Magentotutorial\Weblog\controllers\IndexController.php on line 7"

It means I can not instantiate a model object. Someone help me. This is my code below.

Controller:

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

Model:

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

Config.xml:

    <config>
    <modules>
        <Magentotutorial_Weblog>
            <version>0.1.0</version>
        </Magentotutorial_Weblog>
    </modules>
    <frontend>
        <routers>
            <weblog>
                <use>standard</use>
                <args>
                    <module>Magentotutorial_Weblog</module>
                    <frontName>weblog</frontName>
                </args>
            </weblog>
        </routers>
    </frontend> 
    <models>
        <weblog>
            <class>Magentotutorial_Weblog_Model</class>
            <!-- 
            need to create our own resource, cant just
            use core_mysql4
            -->
            <resourceModel>weblog_mysql4</resourceModel>
        </weblog>
        <weblog_mysql4>
            <class>Magentotutorial_Weblog_Model_Mysql4</class>              
        </weblog_mysql4>
    </models>
</config>

And my database name is blog_posts. Thanks in advance!

like image 465
Ruiwant Avatar asked Sep 03 '26 15:09

Ruiwant


1 Answers

As per my previous comment, you need the mysql4 model class to have the class "talk" to the DB throught the Magento DB Abstraction Layer.

The issue with your model instantiation should be due to the missing tag in your config file:

<config>
    <modules>
        <Magentotutorial_Weblog>
            <version>0.1.0</version>
        </Magentotutorial_Weblog>
    </modules>
    <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>
            <!-- 
            need to create our own resource, cant just
            use core_mysql4
            -->
            <resourceModel>weblog_mysql4</resourceModel>
        </weblog>
        <weblog_mysql4>
            <class>Magentotutorial_Weblog_Model_Mysql4</class>              
        </weblog_mysql4>
    </models>
    </global>
</config>

Please let me know if this solves your problem.

like image 141
Fabrizio D'Ammassa Avatar answered Sep 05 '26 15:09

Fabrizio D'Ammassa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!