yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Extending ActiveRecord vs BaseActiverecord #7170

Closed dynasource closed 9 years ago

dynasource commented 9 years ago

the extension/replacement of ActiveRecord through \Yii::$classMap['yii\db\ActiveRecord'] requires the replacement to have the same functions, as BaseActiveRecord does not have the same functions out of the box.

The combination of \Yii::$classMap and extension and reuse of 'yii\db\ActiveRecord' is unfortunately not possible.

It wouldnt be such a big problem for me, as I could adopt the functions of ActiveRecord in the replacement. The problem is however with updates of Yii. The has been an update to ActiveRecord, which made my replacement ActiveRecord invalid because of strict errors.

How should I approach $classmap & ActiveRecord? The approach is different than with the extension of BaseHelpers.

thanx

samdark commented 9 years ago

What are you trying to achieve with it?

cebe commented 9 years ago

Why don't you just extend from yii\db\ActiveRecord?

dynasource commented 9 years ago

Extending is not practical with repos:

what I want to achieve:

how? a centrally defined ActiveRecord:

practical example:

cebe commented 9 years ago

I do not see why you can not just extend from yii\db\AR and use that extended class in all modules. Replacing the whole AR implementation does not make sense when you want to use most of it.

dynasource commented 9 years ago

I think you are missing a great chance here to enhance Yii2 on flexibility and consistency. Firstly, users cannot use Yii::$classMap for the most vital component of Yii2. This is something I think is very strange, support with the fact that ActiveRecord extension is default from Gii. Secondly, the current implementation of ActiveRecord extending from BaseActiveRecord does not match the style done with the Helpers. This is confusing and leading to problems like I am having.

cebe commented 9 years ago

You are confusing two things here. Helper classes can be replaced via classmap because they are static classes which can not be extended using inheritance. This is only valid use case for the classes in yii\helpers. All other classes can be extended normally using inheritance.

dynasource commented 9 years ago

Alright, thanks. I understand the reasons behind it better now. It was born out of a need. It does show a case of how ActiveRecord could easily be extended by DI. But Ill leave it as it is ;)

dynasource commented 9 years ago

@cebe, how do you look at the scenario I described?

cebe commented 9 years ago

you need to be more specific about what kind of AR classes you get from external packages and what kind of logic you want to inject. Most of what I could think of should be possible using behaviors.

dynasource commented 9 years ago

the Gii generates a model extending from \yii\db\ActiveRecord automatically. This is what I see as something as default and to be expected from 3rd party packages.

About your behavior suggestion: I have worked with behaviors on ActiveRecord extensively and in my latest performance optimizations, I came to the conclusion that I should not work with magic functions in behaviors and put changes directly in ActiveRecord. The performance gain is too significant.

Secondly, there are too many ActiveRecord functions that are interesting to extend. Behaviors cannot always fullfill these needs or dont perform well enough. Examples:

Thirdly, having you're own \yii\db\ActiveRecord enables you to benefit from the power of extension by traits.

Therefore I would suggest to create a blank ActiveRecord as a default class for extending, like simarly has been done for static helpers. The advantage is that you dont have to manually include all current variables & functions in ActiveRecord when you want to use

 \Yii::$classMap['yii\db\ActiveRecord']

IMHO, this would significally contribute to the flexibility of extending Yii2 on one of its main MVC parts which in its current state still lacks optimal extension. With Modules & Controllers you have more configuration options.