为什么magento在心愿单中的模型资源声明中使用了deprecatedNode标记

时间:2014-02-14 11:58:23

标签: magento collections model magento-1.7

我在config.xml中的模型配置中看到了一件有趣的事情。我是未知标签

<deprecatedNode>

在模型类声明中。在自定义扩展中,我们只需要这种类型的模型类声明...

 <models>
            <[module]>
                <class>[Namespace]_[Module]_Model</class>
                <resourceModel>[module]_mysql4</resourceModel>
            </[module]>
            <[module]_mysql4>
                <class>[Namespace]_[Module]_Model_Mysql4</class>
                <entities>
                    <[module]>
                        <table>[module]</table>
                    </[module]>
                </entities>
            </[module]_mysql4>
        </models>

参考 - http://www.magentocommerce.com/wiki/5_-_modules_and_development/0_-_module_development_in_magento/custom_module_with_custom_database_table

But in magento existing modules like Wishlist的config.xml使用 deprecatedNode 定义模型,并使用此模式声明resourceModel ...

<wishlist>
                <class>Mage_Wishlist_Model</class>
                <resourceModel>wishlist_resource</resourceModel>
            </wishlist>

            <wishlist_resource>

                <class>Mage_Wishlist_Model_Resource</class>
                <deprecatedNode>wishlist_mysql4</deprecatedNode>
                <entities>
                    <wishlist>
                        <table>wishlist</table>
                    </wishlist>
                    <item>
                        <table>wishlist_item</table>
                    </item>
                    <item>
                        <table>wishlist_item</table>
                    </item>
                    <item_option><table>wishlist_item_option</table></item_option>
                </entities>
            </wishlist_resource>

为何在MAGENTO中使用此类代码

<resourceModel>wishlist_resource</resourceModel>
      <wishlist_resource>

Mage_Wishlist_Model_Resource                     的 wishlist_mysql4

1 个答案:

答案 0 :(得分:1)

在1.6之后的Magento版本中,资源模型取自节点<..._resource>。 保留<deprecatedNode>是为了与旧的扩展保持向后兼容,旧扩展仍然使用<..._mysql4>节点作为资源模型。

从文件中可以看到:app/code/core/Mage/Core/Model/Config.php第1230行(适用于Magento 1.7.0版):

// First - check maybe the entity class was rewritten
    $className = null;
    if (isset($config->rewrite->$class)) {
        $className = (string)$config->rewrite->$class;
    } else {
        /**
         * Backwards compatibility for pre-MMDB extensions.
         * In MMDB release resource nodes <..._mysql4> were renamed to <..._resource>. So <deprecatedNode> is left
         * to keep name of previously used nodes, that still may be used by non-updated extensions.
         */
        if ($config->deprecatedNode) {
            $deprecatedNode = $config->deprecatedNode;
            $configOld = $this->_xml->global->{$groupType.'s'}->$deprecatedNode;
            if (isset($configOld->rewrite->$class)) {
                $className = (string) $configOld->rewrite->$class;
            }
        }
    }