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 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错