简体   繁体   English

覆盖Yii 1.1.14框架类而不替换它们

[英]Override Yii 1.1.14 framework classes without replacing them

I have some custom logic I need to insert into CDbCommand, CDbTransaction & CActiveRecord classes that comes with the Yii framework. 我有一些自定义逻辑,我需要插入Yii框架随附的CDbCommand,CDbTransaction和CActiveRecord类。 I can't replace them because I am using the same framework files for other projects. 我无法替换它们,因为我在其他项目中使用了相同的框架文件。

Most of my models are already generated and is extended from CActiveRecord. 我的大多数模型已经生成,并从CActiveRecord进行了扩展。 I know I can easily switch them to my own custom class extended from CActiveRecord. 我知道我可以轻松地将它们切换到从CActiveRecord扩展的我自己的自定义类。 However, the methods that I want to override in CDbCommand would not be filtered into CActiveRecord if I extend the CDbCommand class, and in addition CDbCommand is used by many other Framework classes. 但是,如果我扩展CDbCommand类,则要在CDbCommand中覆盖的方法不会被过滤到CActiveRecord中,此外,许多其他Framework类都使用CDbCommand。 I need all other framework classes to use my overrides of CDbCommand methods. 我需要所有其他框架类来使用CDbCommand方法的替代。

Any ideas? 有任何想法吗?

There has been a similar question in the yii forums , concerning classes like CHtml, which are caled statically all over the framework and in generated Code. yii论坛中存在类似的问题,涉及诸如CHtml的类,这些类在整个框架和生成的代码中都是静态进行校准的。

There seems to be no pretty way, the kind-of-consensus in the discussion was to do the following (example CHtml): 似乎没有很好的方法,讨论中的共识类型是执行以下操作(示例CHtml):

Move CHtml.php to Html.php and rename the class to Html. 将CHtml.php移至Html.php,并将该类重命名为Html。 Create a new, empty CHtml class, that extends Html. 创建一个新的空CHtml类,以扩展Html。 Add your overwrites to the new CHtml class. 将您的覆盖添加到新的CHtml类。

It's not pretty but it works and is reasonably maintanable on Yii updates. 它虽然不漂亮,但是可以正常工作,并且可以在Yii更新中合理维护。

Override 覆盖

To override CDbCommand and CDbTransaction, you should override CDbConnection. 要覆盖CDbCommand和CDbTransaction,您应该覆盖CDbConnection。 Most of my Yii Projects have many Yii classes overridden including classes that are mentioned in your question. 我的大多数Yii项目都覆盖了许多Yii类,包括您的问题中提到的类。 It is a lot of work, but you have few choices. 这是很多工作,但您别无选择。

For me, it is good practice to begin projects with overriding all classes you use. 对我来说,从覆盖您使用的所有类开始项目是一个好习惯。 Once you write your project template with custom ActiveRecord, DbConnection, DbCommand and DbTransaction, Html and widget classes, there will be no need to solve problem of extending Yii classes again. 一旦使用自定义ActiveRecord,DbConnection,DbCommand和DbTransaction,Html和窗口小部件类编写了项目模板,就无需再解决扩展Yii类的问题了。

Fork 叉子

Of course, you always can fork and add custom logic directly to Yii classes or adjust something to allow extend classes easily. 当然,您总是可以直接将自定义逻辑派生并添加到Yii类中,或进行一些调整以轻松扩展类。 Sometimes, it is the simplest solution. 有时,这是最简单的解决方案。

== Correct Answer Added below == ==正确答案添加在下面==

ORIGINAL ANSWER 原始答案

As far as Yii 1.1.* is concerned the best way seems to be to place the overriding versions of the files into a folder such as 就Yii 1.1。*而言,最好的方法似乎是将文件的主要版本放在以下文件夹中:

/protected/component/overridden/ folder /protected/component/overridden/文件夹

and then to bind the application to the custom classes by adding them to import configuration in 然后将其添加到导入配置中,从而将应用程序绑定到自定义类

/protected/config/main.php . /protected/config/main.php

    'import'=>array(
        'application.components.CDbConnection',
        'application.components.CDbTransaction',
        'application.components.CDbConnection',
        ...
    ),

This would then call the custom class files even when called by the Framework files. 然后,即使由Framework文件调用,这也将调用自定义类文件。

CORRECT ANSWER - RESOLVED 正确答案-已解决

What I needed was the following at the end of index.php (in the root of project) 我需要的是index.php结尾的以下内容(在项目的根目录中)

Yii::$classMap= [
    'CActiveRecord' => dirname(__FILE__) . 
        '/protected/components/auditAndOps/CActiveRecord.php',
    'CDbCommand' => dirname(__FILE__) . 
        '/protected/components/auditAndOps/CDbCommand.php',
    'CDbTransaction' => dirname(__FILE__) . 
        '/protected/components/auditAndOps/CDbTransaction.php',
];

Yii::createWebApplication($config)->run();

What this essentially does is to remap the original classMap locations of the respective Framework files, to my custom files in /protected/components/auditAndOps/ 这实际上是将相应Framework文件的原始classMap位置重新映射到/protected/components/auditAndOps/自定义文件。

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

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