Order by child


I have a forums model that has many topics, each topic has many comments. I would like to sort the topics by the create_time in the comments, so the topic with the latest comment will be listed first.

'topics' => array(self::HAS_MANY, 'ForumTopics', 'forum_id', 'condition'=>'topics.type="forum"', 'order'=>'topics.sticky DESC, topics.name ASC'),

This is how i order the topics now, but how do i reorder it using the comments create_time?

You can include other CCriteria properties in the relation, such as with ect. and order by MAX( create_time ). You might want to consider denormalising slightly and storing the last updated information in the forum table and updating it when people comment. This would reduce the load on your database when doing the query.

Yeah, think I’m gonna do it that way, just wanted to know if it was possible :)