繁体   English   中英

我如何使用Magento的集合对象

[英]How do I use Magento's collection objects

我正在研究magento的自定义管理模块,并尝试同时学习magento方法=)。 我目前在CE 1.6版本。

我已经关注了几个教程和文章并设法设置了一个自定义数据库表,我认为我管理了make collection类。 (我应该说我是Zend Framework / Magento的新手和初学者熟练的程序员)。 至少以下代码得到了正确的结果:

$department_collection = Mage::getModel('custom/systemconfig')->getCollection()
                            ->addFilter('name','departments');

当我用$department_collection->getData() var_dump进行var_dump时,我得到一个包含来自我的DB的过滤行的数组。

现在,当我尝试这样做时:

foreach ($department_collection as $department) {
        $department->delete();
    }

我从Magento得到一个例外:

-Warning:include(Mage \\ Upperfield \\ Model \\ Systemconfig.php)[function.include]:无法打开流:D:\\ wamp \\ www \\ magento \\ lib \\ Varien \\ Autoload.php中没有此类文件或目录93

问题是,我想我设置的目录结构错误,但只是不明白它是什么。 跟踪并没有多大帮助,因为它是串联的,只显示相关的信息。

我的目录结构如下所示:

+app
  +code
     +local
       +Namespace
         +Module
           +Model
             +Mysql4
                +Systemconfig
                   Collection.php
                Systemconfig.php
             Systemconfig.php

我加载了我的模型:

//file: ../Mysql4/Systemconfig.php
class Namespace_Module_Model_Mysql4_Systemconfig extends Mage_Core_Model_Mysql4_Abstract{
    protected function _construct()
    {
        $this->_init('module/systemconfig', 'systemconfig_id');
    }
}



// file: ../Mysql4/Systemconfig/Collection.php
class Namespace_Module_Model_Mysql4_Systemconfig_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract {
    protected function _construct()
    {
        $this->_init('namespace/systemconfig');
    }
}

我的config.xml目前看起来像这样(摘录):

<models>
  <module>
        <class>Namespace_Module_Model</class>
    <resourceModel>module_mysql4</resourceModel>
</module>

<module_mysql4>
    <class>Namespace_Module_Model_Mysql4</class>
    <entities>
        <systemconfig>
            <table>module_systemconfig</table>
        </systemconfig>
    </entities>
</module_mysql4>

我的猜测是:我设置了错误结构的集合类,或者我错过了解如何使用集合对象。

那里有什么可以向我解释这里发生了什么? 感谢任何帮助。

最好的问候阿德

您得到的错误来自不正确的xml定义或类命名/文件结构 - 这应该是相同的。 当Autoloader读取您的config.xml并查看您在所述config.xml中告诉它的位置时,它没有找到它正在寻找的文件。

关于集合及其父模型的一些信息,如果它有帮助:

如果您直接使用集合,则应使用:

$_collection = Mage::getResourceModel('namespace/model_collection');

如果您正在使用模型来获取集合的数据,并修改数据(例如,总结,添加其他数据,删除等等),您应该像这样调用模型:

$_collection = Mage::getModel('namespace/model')->getCollection();

无论哪种方式,在config.xml中,您需要定义您正在使用资源模型以及应该使用哪些表(如果您有自定义表)。 您永远不必编写新的集合来与默认的Magento表进行交互,因为已经有可以调用或扩展的集合(不推荐,您应该使用Observers)。

以下是config.xml中正确的集合定义的示例:

<models>
    <modulename>
        <resourceModel>modulename_mysql4</resourceModel>
    </modulename>
    <modulename_mysql4>
        <class>Namespace_ModuleName_Model_Mysql4</class>
        <entities>
            <modulename>
                <table>modulename_custom</table>
            </modulename>
        </entities>
    </modulename_mysql4>
</models>
<resources>
    <modulename_setup>
        <setup>
            <module>Namespace_ModuleName</module>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </modulename_setup>
    <modulename_write>
        <connection>
            <use>core_write</use>
        </connection>
    </modulename_write>
    <modulename_read>
        <connection>
            <use>core_read</use>
        </connection>
    </modulename_read>
</resources>

在这个文件中,我写的内联定义和节点必须这样写,你不能有一个返回,它必须是同一行。 这是Magento自动加载器工作方式的一个小缺点。

希望这可以帮助!

暂无
暂无

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

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