I've just started using Yii and I seem to be hitting a bit of a wall with the statistical relations.
Here's my scenario:
I have two objects, Posts and Comments.
Post has many Comments.
I would like to get all Posts ordered by the number of Comments they have.
I've been able to get a count of the comments for Posts using a statistical relation like so:
// relations() specification in Post model
public function relations()
{
return array(
'creator'=>array(self::BELONGS_TO, 'User', 'userId'),
'comment'=>array(self::HAS_MANY, 'Comment', 'postId',
'with'=>'User')
,'commentCount'=>array(self::STAT, 'Comment', 'postId'),
);
}
and then using the following code to retrieve the list:
// from Post controller
$postList=Post::model()->with('creator', 'commentCount')->findAll($criteria);
but I can't seem to find how to sort the Post object using the count from the Comments.
I currently get around this by using findAllBySql but it really doesn't seem like the most elegant or extensible solution. Any ideas?
Thanks for the reply, will. I'd tried this, but it didn't seem to have any effect on the list of Posts returned. From my understanding, that would order the Comment table but not the Post table.
The above statement will execute three SQLs to bring back all posts together with their comment counts and category counts. Using the lazy loading approach, we would end up with 2*N+1 SQL queries if there are N posts.
And yes, it seems that the counts are separately executed SQLs according to the above statement. So it becomes a little difficult to order our results by the STAT count.
Does the Yii dev team have any suggestions to help?
Any guidance you may be able to provide in implementing the ordering via other methods (e.g. CDbExpression?) will be much appreciated, as i feel this might be widely used.
The ability for example to sort a listing based on the number of views on an item (whereby the views are stored in a ViewLog table), or things like sorting items with the most number of reviews/comments is probably pretty commonplace.