如何在magento中实例化模型 [英] how to instantiate a model in magento

查看:68
本文介绍了如何在magento中实例化模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Magento的新手,目前正在关注教程.

I am new to Magento and currently am following this tutorial.

尽管我听不懂

include(Alan/Storm/Model/Blogpost.php) [function.include]: failed to open stream: No such file or directory

显示错误,我的错误已打开.显然,如果我可以按照下一步中的说明显示模型名称,那么这将不是问题,但是我遗漏了一些东西,请在我的代码上提供一些指导.

error to display, my errors are switched on. Obviously this wouldn't be a problem if I could display the model name as the tutorial states in the next step, but im missing something and need some guidance on my code please.

Alan/Storm/etc/config.xml

Alan/Storm/etc/config.xml

<config>
<modules>
    <Alan_Storm>
        <version>0.1.0</version>
    </Alan_Storm>
</modules>

<models>        
    <storm>
        <class>Alan_Storm_Model</class>            
        <resourceModel>storm_mysql4</resourceModel>
    </storm>
</models>

<frontend>
    <routers>
        <storm>
            <use>standard</use>
            <args>
                <module>Alan_Storm</module>
                <frontName>storm</frontName>
            </args>
        </storm>
    </routers>
</frontend>

Alan/Storm/controller/indexController.php

Alan/Storm/controller/indexController.php

class Alan_Storm_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction() {
    echo 'This is the Alan Storm Module';
}

public function testModelAction() {
    $blogpost = Mage::getModel('storm/blogpost');
    echo get_class($blogpost);
}
}

我尝试将Alan/Storm/Model文件夹保留为空,但是访问 http://magentotest.local/storm/Index/testModel .我也尝试将Blogpost.php添加到Model文件夹,但这没有帮助.

I have tried leaving the Alan/Storm/Model folder empty but I just got a blank screen when I visited http://magentotest.local/storm/Index/testModel. I have also tried to add Blogpost.php to the Model folder, but that has not helped.

我在做什么错了?

编辑!

我已经改变了

    public function testModelAction() {
    $blogpost = Mage::getModel('storm/blogpost');
    echo get_class($blogpost);
}

public function testModelAction() {
    $blogpost = Mage::getModel('storm/blogpost');
    echo get_class($blogposts);
}

为什么这样做?在使用get_class功能之前,我还没有创建一个名为$ blogposts的变量.值得注意的是,我在数据库中的表称为Blogposts,但我看不到如何在脚本中进行此调整.

Why does this work? I havn't created a variable called $blogposts prior to using the get_class funciton. It is worth noting that my table within the databse is called blogposts, but I dont see how that would make this adjustment in the script work.

编辑2

新方法仅显示正在工作的控制器的名称,而不显示model/blogpost.php,这正是我所要求的

The new method only displays the name of the controller im working in, not the model/blogpost.php, which is what I thought i was asking for

推荐答案

根据您提供的代码示例,您将<models/>标记放在错误的位置.

Based on the code sample you provided, you have your <models/> tag in the wrong spot.

您已将其直接放置在<config/>节点下.

You've placed it directly under the <config/> node.

<!-- this is wrong -->
<config>
    <models>     
        <!-- ... -->
    </models>
</config>

它应该在<global/>节点下

<config>
    <global>        
        <models>
            <!-- ... -->
        </models>
    </global>
</config>

您可能已经被代码示例绊倒了,该代码示例仅显示部分片段,而不显示整个文档树.请注意,顶级节点是<global/>

You may have been tripped up by the code sample, which only shows a partial fragment, and not the entire document tree. Notice the top level node is <global/>

这些代码示例可能更清晰-那时我仍在让我的教程写腿.

Those code samples could be clearer — I was still getting my tutorial writing legs under me back then.

这篇关于如何在magento中实例化模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆