Решение следующее...
Менять в модели ничего не надо после генерации. Главное, чтобы все привязки были прописаны?
models/Post.php:
Код: Выделить всё
...
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'postTags' => array(self::HAS_MANY, 'PostTag', 'post_id'),
);
}
...
models/PostTag.php:
Код: Выделить всё
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'tag' => array(self::BELONGS_TO, 'Tag', 'tag_id'),
'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
);
}
...
models/Tag.php:
Код: Выделить всё
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'postTags' => array(self::HAS_MANY, 'PostTag', 'tag_id'),
);
}
...
Во вьюшке views/post/view.php добавляем:
Код: Выделить всё
...
// ====================== Тэги этого поста
$tagProvider = new CActiveDataProvider ('PostTag', array(
'criteria'=>array(
'condition'=>'post_id='.$model->id,
'with'=>array('tag'=>array('select'=>'title, id')),
),
'sort'=>array('defaultOrder'=>'tag.title',
'attributes'=>array('tag.title', 'tag.id')
)
));
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'post-grid',
'dataProvider'=>$tagProvider,
'columns'=>array(
'tag.id',
'tag.title',
),
));
...
Теперь у нас при просмотре карточки Поста выводят все Тэги, привязанные к этому посту.