,如果您使用GII那么你可能会在模型中看到模型之间的关系就像是完成:
/**
* @return \yii\db\ActiveQuery
*/
public function getContent()
{
return $this->hasMany(Content_Tag::className(), ['content_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getContent()
{
return $this->hasMany(Tag::className(), ['tag_id' => 'tag_id'])->viaTable('content_tag', ['content_id' => 'id']);
}
如果你想基于内容和标签表,然后在控制器,你可以使用Content_Tag表保存:
public function actionCreate()
{
$model = new Tag();
$content = new Content();
$content_tag = new Content_tag();
if($model->load(Yii::$app->request->post()) && $model->save()){
$model->save(false);
$content_tag->tag_id = $model->id;
$content_tag->content_id = $model->content_id;
$content_tag->save(false);
if($model->save(false))
{
Yii::$app->getSession()->setFlash('success', 'Created successfully');
return $this->render('create',[
'model' => $model,
'content' => $content,
'content_tag' => $content_tag
]);
}
}
else
{
return $this->render('create', [
'model' => $model,
]);
}
}
您可以使用link()来保存。我也在寻找那个,因为我没有使用它。
你可以看看这里:http://www.yiiframework.com/forum/index.php/topic/52592-handling-many-to-many-relation/ – robsch 2014-10-17 09:28:32