/** * Creates a new PostCategory model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new PostCategory(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'post_id' => $model->post_id, 'category_id' => $model->category_id]); } else { return $this->render('create', ['model' => $model]); } }
public function actionCreate() { $model = new PostCategory(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['list']); } else { $model->status = PostCategory::STATUS_ENABLED; return $this->render('create', ['model' => $model]); } }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $rules = ['parent_id' => 'exists:post_category,id', 'active' => 'boolean', 'attachment' => 'image', 'slug' => 'required|alpha_dash|Max:100', 'name' => 'min:1|max:100', 'description' => '', 'meta_description' => '', 'meta_title' => '']; $validator = Validator::make(Input::only(array_keys($rules)), $rules); if ($validator->fails()) { throw new ResourceException($validator->errors()->first()); } DB::beginTransaction(); try { $category = new PostCategory(); $fields = ['active', 'slug']; foreach ($fields as $key => $field) { if (Input::has($field)) { $category->{$field} = Input::get($field); } } //field which can null/empty string $fields = ['description', 'name', 'meta_description', 'meta_title']; foreach ($fields as $key => $field) { if (Input::get($field) === '') { $category->{$field} = null; } elseif (Input::has($field)) { $category->{$field} = Input::get($field); } } $category->save(); if (!Input::has('parent_id')) { $parentCategory = PostCategory::root(); } else { $parentCategory = PostCategory::find(Input::get('parent_id')); } $category->makeChildOf($parentCategory); DB::commit(); return $this->show($category->id); } catch (\Exception $e) { DB::rollback(); throw $e; } }