覆盖Yii 1.1.14框架类而不替换它们 [英] Override Yii 1.1.14 framework classes without replacing them

查看:52
本文介绍了覆盖Yii 1.1.14框架类而不替换它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些自定义逻辑插入CDbCommand,CDbTransaction& Yii框架随附的CActiveRecord类.我无法替换它们,因为我在其他项目中使用了相同的框架文件.

I have some custom logic I need to insert into CDbCommand, CDbTransaction & CActiveRecord classes that comes with the Yii framework. I can't replace them because I am using the same framework files for other projects.

我的大多数模型已经生成,并从CActiveRecord进行了扩展.我知道我可以轻松地将它们切换到从CActiveRecord扩展的我自己的自定义类.但是,如果我扩展CDbCommand类,则要在CDbCommand中覆盖的方法不会被过滤到CActiveRecord中,此外,许多其他Framework类都使用CDbCommand.我需要所有其他框架类来使用我的CDbCommand方法的替代.

Most of my models are already generated and is extended from CActiveRecord. I know I can easily switch them to my own custom class extended from 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. I need all other framework classes to use my overrides of CDbCommand methods.

有什么想法吗?

推荐答案

替代

要覆盖CDbCommand和CDbTransaction,应覆盖CDbConnection.我的大多数Yii项目都覆盖了许多Yii类,包括您的问题中提到的类.这是很多工作,但是您别无选择.

Override

To override CDbCommand and CDbTransaction, you should override CDbConnection. Most of my Yii Projects have many Yii classes overridden including classes that are mentioned in your question. It is a lot of work, but you have few choices.

对我来说,从覆盖您使用的所有类开始项目是一个好习惯.一旦使用自定义ActiveRecord,DbConnection,DbCommand和DbTransaction,Html和窗口小部件类编写了项目模板,就无需再解决扩展Yii类的问题了.

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.

当然,您总是可以直接将分叉逻辑和自定义逻辑添加到Yii类,或进行一些调整以轻松地扩展类.有时,这是最简单的解决方案.

Of course, you always can fork and add custom logic directly to Yii classes or adjust something to allow extend classes easily. Sometimes, it is the simplest solution.

这篇关于覆盖Yii 1.1.14框架类而不替换它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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