Mage::getModel is returning false

2019-07-27 10:41发布

问题:

I am trying to get to grips with calling and using models in magento.

My current task is to simply display a string held in a model by calling it from a controller.

When trying to call SimpleOutput.php I get an error message saying that a non object has been called. I have var_dumped it as you will see and it return false.

I have looked at my code and with my limited understanding of what I need to do in Magento I have everything correct. Obviously i'm missing something. Could someone please take a look and if it's a typo tell where to look and if it's more than a simple spelling mistake explain what ive missed and what I should have done and why?

My code is below

Ts/Firstmodule/etc/config.xml

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

<models>
    <firstmodule>
        <class>Ts_Firstmodule_Model</class>
    </firstmodule>
</models>

<frontend>
    <routers>
        <firstmodule>
            <use>standard</use>
            <args>
                <module>Ts_Firstmodule</module>
                <frontName>firstmodule</frontName>
            </args>
        </firstmodule>
    </routers>
</frontend>
</config>

Ts/Firstmodule/controllers/indexController.php

class Ts_Firstmodule_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
    $simple = Mage::getModel('ts_firstmodule/simpleoutput');
    var_dump($simple);
}
}

Ts/Firstmodule/model/simpleoutput.php

class Ts_Firstmodule_Model_SimpleOutput extends Mage_Core_Model_Abstract
{
public function basicText()
{
    echo 'this is some text from the simple output model inside the basic text function';
}
}

回答1:

You must wrap <models> in <global>, just like this :

<global>
    <models>
        <firstmodule>
            <class>Ts_Firstmodule_Model</class>
        </firstmodule>
    </models>
</global>

Don't hesitate to take a look at the source of the simpler core modules (eg, GoogleAnalytics), to see how they're done and understand the logic behind it.



回答2:

Like always :

Mage::getModel('ts_firstmodule/simpleoutput');

When you do a getModel / getBlock / helper / etc

The first part of the parameter string is the XML node of the layer definied in the config.xml The second part is the full path to your file from the layer folder container.

So, in your case : Mage::getModel('firstmodule/simpleoutput'); should load Ts/Firstmodule/Model/Simpleoutput.php

Note : be carefull of the case of your resources (take a look a standard magento for good practices) !



回答3:

You should modify the config.xml file and add a <global> tag around your <models> tag:

<global>
    <models>
        <firstmodule>
            <class>Ts_Firstmodule_Model</class>
        </firstmodule>
    </models>
<global>

After this, in order to instantiate a model use it like this:

Mage::getModel('firstmodule/simpleoutput')

The first part of the getModel method (up until /) should be the tag name you set in config.xml right under <models> tag. In your case firstmodule.



标签: magento