/** * Creates a new Category model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Category(); $res = false; if ($model->load(Yii::$app->request->post()) && $model->validate()) { /** Handle uploaded file **/ $image = UploadedFile::getInstance($model, 'imageFile'); if ($image instanceof UploadedFile) { $suffix = ''; while (!file_exists($filePath = Yii::getAlias("@webroot/" . Category::REPOSITORY . ($fileName = $image->baseName . $suffix . $image->extension)))) { $suffix += 1; $image->saveAs($filePath); } $model->image = $fileName; } /** Handle tree relation **/ $parent = Category::findOne($model->parent); if ($parent) { $model->appendTo($parent); $res = $model->save(); } else { $res = $model->makeRoot(); } } if ($res) { Yii::$app->session->setFlash('success', Yii::t('bizgod', 'Successfully create new category!')); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('createCategory', ['model' => $model]); } }