1

Mage::getResourceModel()除了使用集合时,是否有任何情况需要 Magento 开发人员使用?

我现在在 Magento 开发了一段时间,还没有遇到需要自己使用的情况Mage::getResourceModel()

4

2 回答 2

2

当您使用不属于标准 CRUD 模式的模型/资源模型对,并且您想直接调用资源模型的公共方法时。

代码库中的一些示例(在我的脑海中)

Mage/Admin/Model/Session.php
100:                $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
138:            $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());

Mage/Adminhtml/Block/Catalog/Product/Attribute/Set/Main.php
179:        $configurable = Mage::getResourceModel('catalog/product_type_configurable_attribute')
191:            $nodeChildren = Mage::getResourceModel('catalog/product_attribute_collection')
233:        $collection = Mage::getResourceModel('catalog/product_attribute_collection')
243:        $attributes = Mage::getResourceModel('catalog/product_attribute_collection')
于 2012-08-14T21:18:25.500 回答
0

getResourceModel当您只想获取资源模型实例时应该使用(这也是对集合的某种优化,但集合是资源模型真的很愚蠢)。例如,为了获取资源模型,您可以使用:

Mage::getModel('my_module/path_to_resource_model');

此代码使用名称创建模型My_Module_Model_Path_To_Resource_Model。但这不是一个好的解决方案,因为您可以在 config.xml 中指定资源模型类前缀(例如在 Mage_Cms 中):

............................................
        <models>
            <cms>
                <class>Mage_Cms_Model</class>
                <resourceModel>cms_mysql4</resourceModel>
            </cms>
            <cms_mysql4>
<!-- ----> --> <class>Mage_Cms_Model_Mysql</class>
............................................

但是当您使用Mage::getResourceModel系统读取资源模型类前缀时(并且您将来可以更改它)。因此,第一个代码片段可以重写为:

Mage::getResourceModel('my_module/model');
于 2012-08-14T21:24:33.270 回答