How to count Items of related object

Hi All,

I want to count total items of related object.

I have the following relations

In Categories Model




			'Plans'=>array(self::HAS_MANY,'plans','category_id'),



In Plans Model




			'Categories'=>array(self::HAS_ONE,'categories','category_id'),



Please let me know How I can count total Plans does a category have?

Make it worked Using

countByAttributes as




Plans::model()->countByAttributes(array('category_id'=>$category->category_id))



Anyone knows the better way to do it?

Did you look at statistical queries? Before you use them, you should probably make sure, you understand, what they do: They can increase the number of queries sent to your DB. Actually i never use them and instead use group + a custom select field to get the count of related items in a single query. Note, that with GROUP BY you can only count the number of objects for one relation.

Thanks mike.