2

This is my first module. The module loads and the index controller executes. I am failing loading my first "hello world" model. I cleared the cache and turned off caching.

I included all the code below, the 2 config files, the controller and the model

I have been working on this for 2 days reading all I can trying every version of upper and lower case. It must be something simple that I am overlooking. Help

thanks

app/etc/modules/Nat_Products.xml

<?xml version="1.0" encoding="UTF-8"?>  
<config>  
    <modules>  
        <Nat_Products>  
            <active>true</active>  
            <codePool>local</codePool>  
        </Nat_Products>  
    </modules>  
</config>

=====

app/code/local/Nat/Products/etc/config.xml

<?xml version="1.0" encoding="UTF-8"? >  
<config >      
    <modules >  
        <Nat_Products >  
            <version >0.1.0</version >  
        </Nat_Products >  
    </modules >  

    <global >  
        <models >  
            <nat_products >  
                <class >Nat_Products_Model</class >  
            </nat_products >  
        </models >  
    </global >          

    <frontend >  
        <routers >  
            <addproduct >  
                <use >standard</use >  
                <args >  
                    <module >Nat_Products</module >  
                    <frontName >addproduct</frontName >  
                </args >  
            </addproduct >  
        </routers >    
    </frontend >      
</config >   

=====

app/code/local/Nat/Products/controllers/IndexController.php

<?php  

class Nat_Products_IndexController extends Mage_Core_Controller_Front_Action {  
    public function indexAction() {  

        echo 'Hello World from ' . __CLASS__ . "<br\>";  

        $product = Mage::getModel("products/addproduct");  // returns false  
        echo $product->hello();  
    }  
} 

=====

app/code/local/Nat/Products/Model/Addproduct.php

<?php  
class Nat_Products_Model_Addproduct extends Mage_Core_Model_Abstract  
{  

    public function hello()  
    {  
        return "hello world from " .__CLASS__;  
    }  

}  

=========

I run maglocal.com/addproduct result of run is:

Hello World from Nat_Products_IndexController ( ! ) Fatal error: Call to a member function hello() on a non-object in maglocal.com\app\code\local\Nat\Products\controllers\IndexController.php on line 9

3

You should instantiate your model like this:

$product = Mage::getModel("nat_products/addproduct");

The part before the slash is the tag you set in your module under the <models> tag:

<models>
    <nat_products><!-- THIS ONE -->
        <class>Nat_Products_Model</class>
    </nat_products>
</models>

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.