recursive statistical query

Hello Yii fellows,

hope, you can help me. I have two Models, "Categories" and "SubCategories", a category has many subcategories, an foreign key (categoryID) exists in SubCategories table. Furthermore I have projects, belonging to exactly one subcategory and thus implicitly one category.

shortly, the relations are:

Category --HAS_MANY–> SubCategory --HAS_MANY–> PROJECT

PROJECT -->HAS_ONE–> SubCategory --HAS_ONE–> Category

Following the guide in section "Statistical Query" it was an ease to get the count of projects for a certain subcategory.

The problem is that I need the project count for a certain category as well on another place. I could simply achieve this by adding the project counts for every subcategory of the current category, but I was wondering, if it is possible to utilize the STAT skills of Yii, doing this kind of recursive statistical query for me.

Best regards,


I am in a similar situation, requiring a stat query on a second level related value. This seems to be a common use case.

this will help

Gustavo, can you please tell us how to use through specifically for this problem? I’ve a similar problem which I explained here:

Do you think through can be used for such a requirement? or are there any other alternatives?