简体   繁体   English

Magento-getModel()和getResourceModel

[英]Magento - getModel() and getResourceModel

I'm developing news extension for Magento following official Guide by Magento and book "Magento PHP Developers Guide". 我正在根据Magento的官方指南和《 Magento PHP开发人员指南》一书为Magento开发新闻扩展。

In the book there are registry entities, items and type - and I think this will be a good practice for the future to add maybe Categories to the News extension. 本书中有注册表实体,项目和类型-我认为这对于将来在新闻扩展中添加类别可能是一个好习惯。 For now I just have Item table that has only news items in it. 现在,我只有Item表,其中只包含新闻项。

According to the book I created the folder structure and placed my files like this: 根据这本书,我创建了文件夹结构,并按如下所示放置了文件:

-Model
--Mysql4
---Item
----Collection.php
---Item.php
--Resource
----Setup.php
--Item.php

in the official guide there's: 在官方指南中有:

-Model
--Resource
---Item
----Collection.php
---Item.php
--Item.php
  1. What is the difference between these two? 两者有什么区别? Are they both EAV? 他们都是EAV吗?

I have setup everything (installer scripts) following the book and it worked just fine. 在本书之后,我已经设置了所有内容(安装程序脚本),并且一切正常。 Now I have Controller, Block and template file and I'm trying to display the data from the database. 现在,我有了Controller,Block和模板文件,并且试图显示数据库中的数据。

My config.xml looks like this: 我的config.xml看起来像这样:

<global>
    <models>
        <gott_news>
            <class>Gott_News_Model</class>
            <resourceModel>gott_news_mysql4</resourceModel>
        </gott_news>
        <gott_news_mysql4>
            <entities>
                <item>
                    <table>gott_news_item</table>
                </item>
            </entities>
        </gott_news_mysql4>
    </models>
    <resources>
        <gott_news_setup>
            <setup>
                <module>Gott_News</module>
                <class>Gott_News_Model_Resource_Setup</class>
            </setup>
        </gott_news_setup>
    </resources>   

Following the book it retrives data from DB like this: 继本书之后,它像这样从数据库检索数据:

$collection = Mage::getModel('gott_news/item')->getCollection();

the guide does it: 该指南做到了:

$newsCollection = Mage::getResourceModel('gott_news/item_collection');
$newsCollection->prepareForList($this->getCurrentPage);

This prepareForList() function is located in the Collection.php file. 此prepareForList()函数位于Collection.php文件中。 I actually should end up this prepareForList() to pass a variable of currentPage 我实际上应该结束这个prepareForList()以传递currentPage的变量

The Problem: Neither works for me. 问题:两者都不适合我。 It doesn't get any results from DB. 它没有从数据库获得任何结果。 and when I use prepareForList() it says it's non-object 当我使用prepareForList()时说它不是对象

In the book, there's also 在书中,还有

    <resources>
        <mdg_giftregistry_setup>
            <setup>
                <module>Mdg_Giftregistry</module>
                <class>Mdg_Giftregistry_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </mdg_giftregistry_setup>
        <mdg_giftregistry_write>
            <connection>
                <use>core_write</use>
            </connection>
        </mdg_giftregistry_write>
        <mdg_giftregistry_read>
            <connection>
                <use>core_read</use>
            </connection>
        </mdg_giftregistry_read>
    </resources>

Is this needed to get data from DB? 从数据库获取数据是否需要?

In the indexController.php indexAction() I'm trying to get Data but nothing is being returned: indexController.php indexAction()我尝试获取数据,但未返回任何内容:

$blogpost = Mage::getModel('gott_news/item')->getCollection();

What should I do to make it work??? 我应该怎么做才能使其正常工作???

I had the same problem before and it seems that the mysql4 calling is the old fashion when ressources are the one to be used now. 我以前也遇到过同样的问题,当资源现在是使用的资源时,似乎mysql4调用是旧的方式。 I use something like that and it works just fine. 我使用类似的东西,并且效果很好。 I don't know if it is compatible with the versions before 1.7 我不知道它是否与1.7之前的版本兼容

        <models>
        <gottnews>
            <class>Gott_News_Model</class>
            <resourceModel>gottnews_resource</resourceModel>
        </gottnews>    
        <gottnews_resource>
            <class>Gott_News_Model_Resource</class>
            <entities>
                <item>
                    <table>gott_news</table>
                </item>
            </entities>
        </gottnews_resource>
    </models>
    <resources>
        <gott_news_setup>
            <setup>
                <module>Gott_News</module>
                <class>Mage_Core_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </gott_news_setup>
        <gottnews_write>
            <connection>
                <use>core_write</use>
            </connection>
        </gottnews_write>
        <gottnews_read>
            <connection>
                <use>core_read</use>
            </connection>
        </gottnews_read>
    </resources>

In the table core_ressource, you need to find the name of your module with its version. 在表core_ressource中,您需要找到模块的名称及其版本。 If it is not, the setup script did not run correctly and you need to check var/log/ for more info if you have logging activated in magento. 如果不是,则安装脚本无法正确运行,如果您在magento中激活了日志记录,则需要检查var / log /以获取更多信息。 if it is but you need to run again the setup, delete the line. 如果是,但是您需要再次运行安装程序,请删除该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM