Making relational AR easier

I am thinking to introduce two more changes to AR to make relational query easier to use. Because they may break some existing code, I would like to hear your opinions. Thanks!

  1. Use relation name as table alias by default. Currently, Yii will automatically generate an alias (e.g t1) if the ‘alias’ option is not specified in the relation declaration. I realized in most cases, if we want to filter according to related table columns, we would need to set their aliases, which is very inconvenient. Using automatic alias has the benefit that it eliminates the possibility of duplicated table aliases. But I think this benefit doesn’t offset its drawback.

  2. Use "join together()" by default. Currently, by default Yii will break a relational query into several ones depending on the number of HAS_MANY and MANY_MANY relations. This is mainly to improve performance. A drawback is that it becomes impossible to specify filtering conditions on the related table columns. Therefore, we have to call together() after with() to ensure Yii don't break the query. I realized that such filtering usage is very common. So I think it would be better if we "join together" by default. If one wants to optimize the query, he can choose not to "join together".

1.- I think yhis is very good, another good point is that when we see the log the query is more readable… I think too that remain the posibility to specify other alias than the default…

2.- I thinks is good too…

I think both are interesting and useful. It will break some codes, but this behavior will be better.

  1. Yes! (it took me a while to figure out to start with).  I actually can't see any drawbacks, are there any ?

  2. I think it might make it easier to use, so yes.  Perhaps the documentation hasn't been very clear on "how to filter related table columns".

Also, if this new way will be the default, it would make sense to have a "optimize tip" in the documentation, saying that if you don't need to filtering in related tables then to use apart() (or whatever it will be called)

in my opinion, good! go ahead!


  1. I support this change as long as we can set another alias manually. This is necessary to be able to have different relations to the same table. Can be useful in some cases.

Thank you all for your suggestions.

I just checked in the code for these changes. Yes, the 'alias' option is still available so that we can explicitly specify the table alias.

Please let me know if you experience any problem with these changes. Thanks!

I'm not a Yii veteran.

But all these changes will conflit with olders already made?

It's easy to upgrade?


Because they may break some existing code

If 1.1 will break some existing code, let it be. Best if yii be without "historical reasons".

I had some problems when migrate Rails 1.3 to 2.0.

Fatal error in the entire website!

There should be at least a step-by-step migrating to new code.

But I agree with:


If 1.1 will break some existing code, let it be. Best if yii be without "historical reasons".