/** * Finds the GoodsCategory model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return GoodsCategory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = GoodsCategory::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionCatedit() { //显示编辑表单 if (Yii::$app->request->get('id')) { $id = Yii::$app->request->get('id'); //获取所有的分类信息以填充下拉列表 $model = new GoodsCategory(); $cate = $model->list_cate(); #获取当前这条记录的信息 $model = GoodsCategory::findOne($id); return $this->render('catedit', ['cat_id' => $id, 'cate' => $cate, 'model' => $model]); //修改类别 } elseif (Yii::$app->request->post('id')) { $id = Yii::$app->request->post('id'); //获取当前表单提交的cat_id下的所有后代分类 $model = new GoodsCategory(); $cate = $model->list_cate($id); //获取这些后代分类的cat_id $sub_ids = []; foreach ($cate as $v) { $sub_ids[] = $v['cat_id']; } //获取当前表单提交parent_id $pid = Yii::$app->request->post(); $parent_id = $pid['GoodsCategory']['parent_id']; //判断所选的父分类是否为当前分类或其后代分类 if ($parent_id == $id || in_array($parent_id, $sub_ids)) { Yii::$app->getSession()->setFlash("info", '编辑失败,不能将分类放置到当前分类或其子分类!'); return $this->redirect(['catedit', 'id' => $id]); } else { $model = GoodsCategory::findOne($id); if ($model === null) { Yii::$app->getSession()->setFlash("info", '编辑失败'); return $this->redirect(['catlist']); } if ($model->load(Yii::$app->request->post())) { if ($model->save()) { Yii::$app->getSession()->setFlash('info', '编辑成功!'); return $this->redirect(['catlist']); } else { Yii::$app->getSession()->setFlash('info', '编辑失败!'); return $this->redirect(['catlist']); } } } } }