A way to order using a column in a different table

I have three tables.

user(id, username, password, email, website)

post (id, title, content, commentCount, timeStamp, AdderID)

comments (userid, postid, comment, timeStamp)

To display all the posts a user has commented on, I am doing the following:

in my site controller:


'comments' = comments::model()->findall($criteria)

in my index


foreach($comments AS $comments):

<?php $this->renderPartial('/post/show',array('model'=>$comments->eid)); ?>

$Votes->eid is a relationship defined by:


'eid' => array(self::BELONGS_TO, 'Entry', 'entryid'),

I want to be able to organise the list of posts in different ways. I am able to organise them in relation to time because of the timeStamp on the comments. I want to be able to organise them in relation to the amount of comments a post has. I can’t think of a way to do this.

Does anyone have any ideas on how I could do this? - Or have I just set it up wrong?

Take a look in CSort documentation, maybe it helps: http://www.yiiframework.com/doc/api/CSort

Thanks for the answer. I don’t really understand CSort. What can it do?

Best way to learn is to look some example/demos/etc. Better to understand.