我正在尝试在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';
}
}发布于 2013-07-11 16:48:14
您必须将<models>包装在<global>中,如下所示:
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
</global>请不要犹豫,看看更简单的核心模块(如GoogleAnalytics)的源代码,看看它们是如何完成的,并理解其背后的逻辑。
发布于 2013-07-11 16:47:06
一如既往:
Mage::getModel('ts_firstmodule/simpleoutput');当您执行getModel / getBlock / helper /等时
参数字符串的第一部分是在config.xml中定义的层的XML节点,第二部分是从层文件夹容器到文件的完整路径。
因此,在您的示例中: Mage::getModel('firstmodule/simpleoutput');应该加载Ts/Firstmodule/Model/Simpleoutput.php
注意:要小心你的资源的情况(看看标准magento的良好实践)!
发布于 2013-07-11 19:00:31
您应该修改config.xml文件,并在<models>标记周围添加一个<global>标记:
<global>
<models>
<firstmodule>
<class>Ts_Firstmodule_Model</class>
</firstmodule>
</models>
<global>在此之后,为了实例化一个模型,可以这样使用它:
Mage::getModel('firstmodule/simpleoutput')getModel方法的第一部分(直到/)应该是您在config.xml中<models>标记下设置的标记名。在您的例子中是firstmodule。
https://stackoverflow.com/questions/17588748
复制相似问题