/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); if ($model->load(Yii::$app->request->post()) && $model->save()) { foreach (Category::getCategoriesById($model->categories_id) as $category) { $model->link('categories', $category); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Post model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Post(); $model->user_id = Yii::$app->user->id; if ($model->load(Yii::$app->request->post())) { $postData = Yii::$app->request->post('Post'); if (!empty($postData['categories'])) { foreach ($postData['categories'] as $categoryId) { $category = Category::find()->where(['id' => $categoryId])->one(); $model->link('categories', $category); } } $model->setScenario('create'); $model->poster = \yii\web\UploadedFile::getInstance($model, 'poster'); if ($model->save() && $model->upload()) { return $this->redirect(['view', 'id' => $model->id]); } } return $this->render('create', ['model' => $model]); }