model = new \app\admin\model\shop\GoodsLabel; $this->view->assign("statusList", $this->model->getStatusList()); $this->assignconfig("statusList", json_encode($this->model->getStatusList())); $this->view->assign("styleTypeList", $this->model->getStyleTypeList()); $this->assignconfig("styleTypeList", json_encode($this->model->getStyleTypeList())); } public function getList() { $list = $this->model->field('id,name')->select(); return json($list); } public function index() { $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['group']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as $row) { $row->visible(['id', 'name', 'group_id', 'style_type', 'color_json', 'icon', 'memo', 'weigh', 'status', 'createtime', 'updatetime']); $row->visible(['status_text', 'style_type_text','group.name']); $row->hidden(['deletetime']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post('row/a'); if ($params) { if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } // 检查标签名称是否唯一 $label = $this->model->where('name', $params['name'])->find(); if ($label) { $this->error('标签名称已存在'); } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success('添加成功'); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post('row/a'); if ($params) { // 检查标签名称是否唯一 $label = $this->model->where('name', $params['name'])->where('id', 'neq', $row->id)->find(); if ($label) { $this->error('标签名称已存在'); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success('编辑成功'); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } public function checkLabelNameUnique() { $name = $this->request->post('name'); $id = $this->request->post('id'); $count = $this->model->where('name', $name)->where('id', 'neq', $id)->count(); return $count > 0 ? '标签名称已存在' : '标签名称可用'; } }