/** * Creates a new Articles model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Articles(); $aticleCategory = new CategoryArticle(); $fileName = 'file'; $uploadPath = 'images/articles/'; if ($model->load(Yii::$app->request->post()) && $aticleCategory->load(Yii::$app->request->post())) { $model->id = $model->find()->max('id') + 1; $model->save(); $c = count(Yii::$app->request->post('CategoryArticle')['category_id']); print_r(Yii::$app->request->post('CategoryArticle')['category_id']); for ($i = 0; $i < $c; ++$i) { $newCat = new CategoryArticle(); $newCat->category_id = Yii::$app->request->post('CategoryArticle')['category_id'][$i]; $newCat->article_id = $model->id; $newCat->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { if (isset($_FILES[$fileName])) { $file = \yii\web\UploadedFile::getInstanceByName($fileName); $p = $model->find()->max('id') + 1; $path = $uploadPath . $p; if (!file_exists($path)) { mkdir($path); } if ($file->saveAs($path . '/' . $file->name)) { $articleImg = new ArticlesImg(); $articleImg->article_id = $model->find()->max('id') + 1; $articleImg->img_url = $file->name; $articleImg->save(); echo $path . '/' . $file->name; } } else { return $this->render('create', ['model' => $model, 'aticleCategory' => $aticleCategory]); } } }