【问题标题】:Mage::getModel is returning falseMage::getModel 返回 false
【发布时间】:2013-07-11 08:36:05
【问题描述】:

我正在尝试在 magento 中调用和使用模型。

我当前的任务是通过从控制器调用它来简单地显示模型中保存的字符串。

尝试调用 SimpleOutput.php 时,我收到一条错误消息,指出已调用了一个非对象。如您所见,我已将其 var_dumped 并返回 false。

我查看了我的代码,并且我对在 Magento 中需要做什么的理解有限,我认为一切都是正确的。显然我错过了一些东西。有人可以看一下,如果是拼写错误,请告诉在哪里看,如果不仅仅是一个简单的拼写错误,请解释我错过了什么,我应该做什么以及为什么?

我的代码在下面

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';
}
}

【问题讨论】:

    标签: magento


    【解决方案1】:

    您必须将&lt;models&gt; 包裹在&lt;global&gt; 中,就像这样:

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

    不要犹豫,看看更简单的核心模块的来源(例如,GoogleAnalytics),看看它们是如何完成的并了解其背后的逻辑。

    【讨论】:

    • 感谢您的工作。至于看简单的模块代码,我将开始,最好的想法。
    【解决方案2】:

    一如既往:

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

    当您执行 getModel / getBlock / helper / 等时

    参数字符串的第一部分是config.xml中定义的层的XML节点 第二部分是图层文件夹容器中文件的完整路径。

    所以,在你的情况下:Mage::getModel('firstmodule/simpleoutput');应该加载Ts/Firstmodule/Model/Simpleoutput.php

    注意:请注意您的资源情况(查看标准 magento 以获得良好实践)!

    【讨论】:

      【解决方案3】:

      您应该修改 config.xml 文件并在您的 &lt;models&gt; 标签周围添加一个 &lt;global&gt; 标签:

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

      在此之后,为了实例化一个模型,像这样使用它:

      Mage::getModel('firstmodule/simpleoutput')
      

      getModel 方法的第一部分(直到 /)应该是您在 config.xml 中设置的标签名称,就在 &lt;models&gt; 标签下方。在你的情况下firstmodule

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 2012-11-03
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多