Separation Of Active Record From Main Repo


I know it is late because the alpha version is releasing within couple of days. But I’m feeling strongly that the AR could be distributed separately, so we can use it with other frameworks too. Also separate distribution will set it to compete with doctrine. I believe the built in AR is no way behind the doctrine, as I have tested in preview version. But I think the competition will make it nearly perfect and way better in future developments.

What are your thoughts?

I just skimmed through the files in framework\yii\db. Looks like a lot of other Yii components are being used there. Especially from yii\base and yii\helpers. I’d say it’s still possible to extract the AR implementation among with all the DB stuff. But I’m really not sure if it’s worth the effort. On top of that, I’d predict some speed losses if AR became more modular. I cannot imagine Qiang approving of this.

But don’t take my word for it :rolleyes:

We have no goal making Yii components reusable without Yii.

I agree that Yii2 ActiveRecord implementation is great and very competitive. Recently I was developing framework for Wordpress plugin and needed AR in that MVC framework, so I decided to port Yii2 ActiverRecord to my framework.

From my experience I can say that it isn’t a big problem, I have ported majority of db classes (with some dependencies, such as Object, Component, Model) and refactored/code, stripped some functionality (such as behaviors and events) and everything is working great.

But I don’t see any possibility for full porting of ActiveRecord (with all it’s functionality to separate library). Some things (such as behaviors and events) are strongly dependent on many Yii2 other classes and separation of that concepts will be bad for Yii2 for sure. Yii2 ActiveRecord is so great and lightweight only due to the fact that it is in Yii.

[color=#1C2837][size=2]> Some things (such as behaviors and events) are strongly dependent on many Yii2 other classes[/size][/color]



[color=#1C2837][size=2]Could you give me an example for that? I looked at the code, but found no new dependencies for behaviors and models.[/size][/color]

Unless you consider the size of the framework codebase is too big, you can use AR in other projects by including the whole Yii framework. You just don’t call the run() method of the application instance.

I don’t understand what you mean by saying “new dependencies”, but what I mean is there is code that references

ModelEvent (Event), Validator, Inflector

classes for example in Model (from which ActiveRecord is extending). There is code referencing Behaviour class in Component (from which Model is extending) and etc… Do you need exact examples with urls and lines???

I mean ORM (in our case Yii2 ActiveRecord) don’t necessarily need that functionality. It doesn’t have to have any behaviours or events, or ability to attach events to it to do it’s job. It’s main job to create objects that are related to database models and to manipulate the data with the help of that objects. So in my case when I’ve used Yii2 ActiveRecord in other code, I’ve simply stripped all that code that doesn’t concern ORM directly and made some corrections to remove those dependencies, for example references to


@Arman Poghosyan: Thanks, I think I understand what you mean, although I think Qiang’s approach would do the job quite easy.

@schmunk, Yeah I agree. But for some reasons Wordpress has problems with namespaces and autoloading used in plugin, that’s why I chose to use only the code that I need and not whole framework. I really hope the day will come when Wordpress will start to use OOP and PHP new features (namespacing, traits…). It will ease developers work.

I’m already tired of deprecated notices in error log of wordpress using PHP 5.5.x (It still uses mysql_xxx functions, which are deprecated).

Anyway the point is that we have great framework with great code, that I even port to other projects (CMSs, frameworks) when I need it.

I have learnt a lot from Yii 1 and 2 codebase and I appreciate all the work that core team and contributors do. Thanks to all of them!!!

Thank you guys for all of your replies. I’m following the Qiang’s approach but the thing is I’m feeling it is as a workaround not a official support. Actually it might be my very personal point of view, that I like to see everything modular (like the concept of phoneblocks).

But it seems real separation of AR from Yii might be hard today because it will also need to separate the helpers and other related class from Yii too.