无法覆盖magento核心配置模型 [英] Can't override magento core config model

查看:61
本文介绍了无法覆盖magento核心配置模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can't override magento core config model Mage_Core_Model_Config. I have magento 1.9.2.1. Here is 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

And class Peexl/CustomFlatrate/Model/Core/Config.php

class Peexl_CustomFlatrate_Model_Core_Config extends Mage_Core_Model_Config
{

}

什么也没有发生:(

推荐答案

是的,你不能.

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

Magento's class rewrite system works because almost all Magento objects are instantiated via the Mage::getModel static class. However, if an object is created directly via the new method

$foo = new Some_Class_File_Here;

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

Magento's class rewrite won't be able to replace the class that's instantiated. There's a handful of objects Magento needs to instantiate without the rewrite system. Magento needs to instantiate these classes without the rewrite system because they're the actual classes that implement the rewrite system.

这些类包括

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类.如果您希望修改此类的行为,则有两个选择.

Which includes the Mage_Core_Model_Config class. If you wish to modify the behavior of this class, you have two options.

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

First, you can create a local code pool override

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

,其中包含app/code/copy/Mage/Core/Model/Config.php中类的确切副本以及您的更改.缺点是,每次升级Magento时,您都需要手动更新此类,如果您不小心,可能会破坏核心代码所依赖的功能.

with an exact copy of the class from app/code/copy/Mage/Core/Model/Config.php, plus your changes. The downside of this is you'll need to manually update this class whenever you upgrade Magento, and if you're not careful you may break functionality that core code relies on.

第二个现代版本的Magento 1包含用于替代配置类的选项.看一下Magento在哪里实例化配置选项

Second, modern versions of Magento 1 contain the option for an alternative configuration class. Take a look at where Magento instantiates the configuration option

#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引导程序文件

You can see that Magento looks for a class name in the $options array's config_model key. You can set this via the index.php bootstrap file

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

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

This is slightly better than a local code pool override, as Package_Module_Model_Config can extend the base configuration class, and you can change only what you need. However, it does rely on you maintaining your own index.php bootstrap file, which makes it not great for redistribution.

希望有帮助!

这篇关于无法覆盖magento核心配置模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆