示例#1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPostTypeTaxonomies()
 {
     return $this->hasMany(PostTypeTaxonomy::className(), ['post_type_id' => 'id']);
 }
 /**
  * Updates an existing PostType model.
  * If update is successful, the browser will be redirected to the 'view' page.
  *
  * @param integer $id
  *
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     $taxonomies = ArrayHelper::map(Taxonomy::find()->all(), 'id', 'taxonomy_name');
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         // Delete all PostTypeTaxonomy where post_type_id this id
         PostTypeTaxonomy::deleteAll(['post_type_id' => $id]);
         // Refill PostTypeTaxonomy for this model
         //var_dump(Yii::$app->request->post('PostTypeTaxonomy')); exit;
         if (($postTypeTaxonomy = Yii::$app->request->post('PostTypeTaxonomy')) && ($taxonomyIds = Json::decode($postTypeTaxonomy['taxonomyIds']))) {
             foreach ($taxonomyIds as $taxonomy_id) {
                 $postTypeTaxonomy = new PostTypeTaxonomy();
                 $postTypeTaxonomy->post_type_id = $model->id;
                 $postTypeTaxonomy->taxonomy_id = $taxonomy_id;
                 $postTypeTaxonomy->save();
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     }
     return $this->render('update', ['model' => $model, 'taxonomy' => new Taxonomy(), 'taxonomies' => $taxonomies]);
 }