Count With Join

Hello In need help with count my posts in begin of forum categories list

SELECT COUNT(id) FROM posts a INNER JOIN topics b ON a.tid = WHERE b.fid=$fid this is the old code,

Can I do this count in my forum model by relations or better do it in controller and how it looks in yii style?


Dear Friend

Let us consider an example.

Author and Posts.

Author has many posts.


public function relations()



		return array(

			'posts' => array(self::HAS_MANY, 'Post', 'a_id'),



Now we listing aurhor names.

we can also the number of posts written by him like following.


echo count($author->posts);

We can also finetune a bit.

We want to know how many posts the author written related to YII.


echo count($author->posts(array(

   'condition'=>'title LIKE "%yii%"',