doujiang3997 2014-11-19 14:11
浏览 41
已采纳

Magento - getModel()和getResourceModel

I'm developing news extension for Magento following official Guide by Magento and book "Magento PHP Developers Guide".

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.

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?

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.

My config.xml looks like this:

<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. I actually should end up this prepareForList() to pass a variable of 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

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:

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

What should I do to make it work???

  • 写回答

1条回答 默认 最新

  • dongxiz5342 2014-11-19 15:20
    关注

    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. I use something like that and it works just fine. I don't know if it is compatible with the versions before 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. 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. if it is but you need to run again the setup, delete the line.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?