STAT relations through MANY_MANY relation?

Hi,

Suppose we have a User model/table and a Item model/table, and that there’s a MANY_MANY relation between them through a “like” table (i.e. a user likes an item).

Is it possible to define a STAT relation in Item to get the count of users who like the item (i.e. the ‘likes’ count)?

If so how would you define the relation in the relations() method?

And then, are STAT relations supposed to be taken into account in relational queries? I am under the impressiona that when you create a CDbCriteria with a with property that includes various relations and a STAT relation, the stat relation is completely ignored. (and yes, I’m setting together=true).

What I’d like to be able to do is to define the abovementioned likes count as a STAT relation for items, which by default would give a total like count, but then be able to do complex queries involving other relations, imposing specifical conditions so that the likes count get narrowed down (e.g. number of users from a given country that like an item).

By the way, is it possible to access child-relations of a STAT relation in a with?

>> Is it possible to define a STAT relation in Item to get the count of users who like the item (i.e. the ‘likes’ count)?

you can do a join query and use group by condition to get the count

If so how would you define the relation in the relations() method?

you can something like following


// your user model

'items'=>array(self::MANY_MANY, 'Item',

                'your_join_table(user_id, item_id)')

And then, are STAT relations supposed to be taken into account in relational queries? I am under the impressiona that when you create a CDbCriteria with a with property that includes various relations and a STAT relation, the stat relation is completely ignored. (and yes, I’m setting together=true).

yes you can

If you ask me, that’s far too inefficient. What about this one:




class Item ...{


function relations(){ 

...

'userLike' => array(self::STAT, 'Item', 'user_item(item_id)')

...

}



Though I haven’t tried if he accepts this syntax.

Regards,