/** * @return \yii\db\ActiveQuery */ public function getTerms() { return $this->hasMany(BlogTermRelationships::className(), ['post_id' => 'id']); }
/** * Updates an existing BlogPosts model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { BlogAssets::register($this->view); FilemanagerTinyAssets::register($this->view); $model = $this->findModel($id); $terms = new BlogTerms(); $categories = BlogTerms::find()->termType()->orderBy('name')->all(); if ($model->load(Yii::$app->request->post())) { if ($model->validate() && $model->save()) { BlogTermRelationships::deleteAll('post_id = ' . $model->id); $termRelations = new BlogTermRelationships(); $categoryTerms = Yii::$app->request->post(); if (isset($categoryTerms['categories'])) { foreach ($categoryTerms['categories'] as $c) { $termRelations->isNewRecord = true; $termRelations->id = null; $termRelations->post_id = $model->id; $termRelations->term_id = $c; $termRelations->save(); } } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'terms' => $terms, 'categories' => $categories]); } } else { return $this->render('update', ['model' => $model, 'terms' => $terms, 'categories' => $categories]); } }