无法覆盖magento核心配置模型

时间:2016-03-04 18:51:56

标签: php magento rewrite override core

无法覆盖magento核心配置模型Mage_Core_Model_Config。我有magento 1.9.2.1。 这是config.xml

<global>
    <helpers>
        <peexl_customflatrate>
            <class>Peexl_CustomFlatrate_Helper</class>
        </peexl_customflatrate>
            </helpers>
    <models>
        <peexl_customflatrate>
            <class>Peexl_CustomFlatrate_Model</class>
        </peexl_customflatrate> 
                    <core>
                        <rewrite>
                             <config>Peexl_CustomFlatrate_Core_Config</config>
                        </rewrite> 
                    </core>    

类Peexl / CustomFlatrate / Model / Core / Config.php

class Peexl_CustomFlatrate_Model_Core_Config extends Mage_Core_Model_Config
{

}

什么都没发生:(

2 个答案:

答案 0 :(得分:10)

没错,你不能。

Magento的类重写系统可以工作,因为几乎所有的Magento对象都是通过Mage::getModel静态类实例化的。但是,如果通过new方法直接创建对象

$foo = new Some_Class_File_Here;

Magento的类重写将无法替换已实例化的类。在没有重写系统的情况下,Magento需要实例化一些对象。 Magento需要在没有重写系统的情况下实例化这些类,因为它们是实现重写系统的实际类。

这些课程包括

self::$_objects = new Varien_Object_Cache;        
self::$_app     = new Mage_Core_Model_App();    
self::$_events  = new Varien_Event_Collection();    
self::$_config  = new Mage_Core_Model_Config($options);

其中包括Mage_Core_Model_Config课程。如果您希望修改此类的行为,您有两种选择。

首先,您可以创建本地代码池覆盖

app/code/local/Mage/Core/Model/Config.php

包含来自app/code/copy/Mage/Core/Model/Config.php的类的精确副本,以及您的更改。这样做的缺点是,无论何时升级Magento,您都需要手动更新此类,如果您不小心,可能会破坏核心代码所依赖的功能。

其次,现代版本的Magento 1包含备用配置类的选项。看看Magento实例化配置选项

的位置
#File: app/Mage.php
protected static function _setConfigModel($options = array())
{
    if (isset($options['config_model']) && class_exists($options['config_model'])) {
        $alternativeConfigModelName = $options['config_model'];
        unset($options['config_model']);
        $alternativeConfigModel = new $alternativeConfigModelName($options);
    } else {
        $alternativeConfigModel = null;
    }

    if (!is_null($alternativeConfigModel) && ($alternativeConfigModel instanceof Mage_Core_Model_Config)) {
        self::$_config = $alternativeConfigModel;
    } else {
        self::$_config = new Mage_Core_Model_Config($options);
    }
}    

您可以看到Magento在$options数组的config_model键中查找类名。您可以通过index.php引导程序文件

进行设置
#File: index.php
Mage::run($mageRunCode, $mageRunType, array('config_model'=>'Package_Module_Model_Config'));

这比本地代码池覆盖略好,因为Package_Module_Model_Config可以扩展基本配置类,并且您只能更改所需内容。但是,它确实依赖于您维护自己的index.php引导程序文件,这使得它不适合重新分发。

希望有所帮助!

答案 1 :(得分:0)

首先,您的重写不会起作用,因为它的设置方式错误。 config.xml中的重写类名称与文件中的类名不匹配,有单词&#34; Model&#34;失踪。 它应该是:

                <rewrite>
                     <config>Peexl_CustomFlatrate_Model_Core_Config</config>
                </rewrite>