modelClass === null) { throw new InvalidConfigException('"modelClass" 属性必须设置.'); } } /** * 首页 * * @return mixed */ public function actionIndex() { $data = $this->modelClass::find() ->where(['>=', 'status', StatusEnum::DISABLED]); $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]); $models = $data->offset($pages->offset) ->orderBy('id desc') ->limit($pages->limit) ->all(); return $this->render($this->action->id, [ 'models' => $models, 'pages' => $pages ]); } /** * 编辑/创建 * * @return mixed */ public function actionEdit() { $id = Yii::$app->request->get('id', null); $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->referrer(); } return $this->render($this->action->id, [ 'model' => $model, ]); } /** * 伪删除 * * @param $id * @return mixed */ public function actionDestroy($id) { if (!($model = $this->modelClass::findOne($id))) { return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error'); } $model->status = StatusEnum::DELETE; if ($model->save()) { return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer)); } return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error'); } /** * 删除 * * @param $id * @return mixed * @throws \Throwable * @throws \yii\db\StaleObjectException */ public function actionDelete($id) { if ($this->findModel($id)->delete()) { return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer)); } return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error'); } /** * ajax更新排序/状态 * * @param $id * @return array */ public function actionAjaxUpdate($id) { if (!($model = $this->modelClass::findOne($id))) { return ResultHelper::json(404, '找不到数据'); } $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']); if (!$model->save()) { return ResultHelper::json(422, $this->getError($model)); } return ResultHelper::json(200, '修改成功'); } /** * ajax编辑/创建 * * @return mixed|string|\yii\web\Response * @throws \yii\base\ExitException */ public function actionAjaxEdit() { $id = Yii::$app->request->get('id'); $model = $this->findModel($id); // ajax 校验 $this->activeFormValidate($model); if ($model->load(Yii::$app->request->post())) { return $model->save() ? $this->redirect(Yii::$app->request->referrer) : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error'); } return $this->renderAjax($this->action->id, [ 'model' => $model, ]); } /** * 返回模型 * * @param $id * @return \yii\db\ActiveRecord */ protected function findModel($id) { /* @var $model \yii\db\ActiveRecord */ if (empty($id) || empty($model = $this->modelClass::findOne($id))) { $model = new $this->modelClass; return $model->loadDefaultValues(); } return $model; } }