If you created models using gii, then you can see in the model that relations are performed as follows:
public function getContent() { return $this->hasMany(Content_Tag::className(), ['content_id' => 'id']); } public function getContent() { return $this->hasMany(Tag::className(), ['tag_id' => 'tag_id'])->viaTable('content_tag', ['content_id' => 'id']); }
If you want to save the table Content_Tag based on the table of content and tags, then in the controller you can use:
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, ]); } }
You can use the link () to save. I am also looking for this as I have not used this.
source share