Take the 2-minute tour ×

I have three models (tables):

  1. content
  2. category
  3. contentCategory

The contentCategory have content_id and category_id that related to each other.

How I must write relation in each model to select content of specific category?

public function relations()
    return array(
share|improve this question
if you define the relationship(PK, FK) while designing the database, yii will automatically build the relationship, which is better than defining manually... yiiframework.com/doc/guide/1.1/en/database.arr the above doc explains cleary about building relations.... –  Neeraj Dangol Jan 20 '13 at 9:50
add comment

1 Answer

up vote 2 down vote accepted

If your table contentCategory has only 2 fields content_id and category_id then u don't need to create a model for this table and in the other models you'll have


                'contentCategory(content_id, category_id)'),


'contents'=>array(self::MANY_MANY, 'Content',
                    'contentCategory(category_id, content_id)'),

If your table contentCategory has more content then you'll need to use through:

Link to the guide

share|improve this answer
add comment

Your Answer


