Ability to chain multiple with method calls

It would be very nice to be able to chain multiple ‘with’ method calls on CActiveRecord. For example, one could do:




$finder = Post::model()->with('author');

if ($detailedView)

{

  $finder = $results->with('comments');

}

$results = $finder->findAll();



With’ returns a CActiveFinder. My thought is about adding a ‘with’ method there which calls buildJoinTree. I haven’t tested my thought though so I don’t really know whether it breaks any functionality.




$relations = array('author');


if ($detailedView)

  $relations[] = 'comments';


$results = Post::model()->with($relations)->findAll();



Thank you for your suggestion. The current design of relational AR is not very good. As a result, it is not easy to implement this.

As a workaround to your problem, you may consider the following:




$with=$detailedView ? array('author','comments') : 'author';

$results=Post::model()->with($with)->findAll();



Thanks for the prompt reply both of you guys. Actually that is the thing that I am doing right now, like your proposals. It’s fine, it works OK but looks a bit like a workaround. Anyway, it’s not something of high priority. It’s more like something to keep in mind if ever the time comes to improve the design of relational AR.

I agree, it would be nice to see more 101 OO techniques used and far less use of arrays. (I’m not mad about the prevalence of chaining either, tbh.)

Probably my #2 gripe about Yii is that writing readable code (i.e. easily comprehensible) is quite difficult.

I think Yii is plenty readable… more readable then cakephp especially, where it was really hard to follow the execution flow.