Making relational AR easier
Posted 10 June 2009 - 06:57 AM
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".
Posted 10 June 2009 - 11:03 AM
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)
Posted 13 June 2009 - 03:06 PM
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!
Posted 22 June 2009 - 12:44 PM
Fatal error in the entire website!
There should be at least a step-by-step migrating to new code.
But I agree with: