how to make this relation for my model

I have 3 models as below




 class User extends CActiveRecord

{

    public function relations()

    {

        return array(

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

        );

    }

}


class Post extends CActiveRecord

{

    public function relations()

    {

        return array(

'tags' => array(self:: MANY_MANY, 'tag', 'post_tag(tag_id, post_id)'),

        );

    }

}


class Tag extends CActiveRecord

{

    public function relations()

    {

        return array(

            'posts' => array(self::MANY_MANY, 'post', 'post_tag(tag_id, post_id)'),

        );

    }

}

so the models are user <- post <- post_tag -> tag

how to make relation in user model to count all the tags related to the user ?

Thank you

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#statistical-query

Thank you,

but this will only get me the count of tags from post, but I want from user how to reach from user model to the tags count?

thank you

Perhaps somebody else can answer this better. (4 years since I used PHP/Yii 1.x/SQL)

I think it should be easy to get the tag count related to a specific post by a specific user. Just add a BELONGS_TO User relation to Post and query Post.

Did you mean a distinct count of all tags for a specific user?

I think you can get the PostTag (HAS_MANY) or Tag(MANY_MANY) models (possibly in a subquery) and do a DISTINCT query on the tag_id’s.

Have a look at CDbCriteria. Look for old discussions (e.g Google site:yiiframework.com … ).