/** * Creates a new PostType and PostTypeTaxonomy model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return mixed */ public function actionCreate() { $model = new PostType(); $taxonomies = ArrayHelper::map(Taxonomy::find()->all(), 'id', 'name'); if ($model->load(Yii::$app->request->post()) && $model->save()) { $postTypeTaxonomy = Yii::$app->request->post('PostTypeTaxonomy'); if ($taxonomyIds = Json::decode($postTypeTaxonomy['taxonomyIds'])) { foreach ($taxonomyIds as $taxonomyId) { $postTypeTaxonomy = new PostTypeTaxonomy(); $postTypeTaxonomy->setAttributes(['post_type_id' => $model->id, 'taxonomy_id' => $taxonomyId]); $postTypeTaxonomy->save(); } } return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', ['model' => $model, 'taxonomy' => new Taxonomy(), 'taxonomies' => $taxonomies]); }