model = new \app\admin\model\Cars; $series = Db::query("SELECT a.id,brand_id as pid, a.name FROM series a LEFT JOIN brands b ON a.brand_id = b.id;"); $brands = Db::query("SELECT id, 0 as pid, `name` FROM brands;"); $this->series = $series; $this->brands = $brands; $data = array_merge($brands,$series); $tree = $this->buildTree($data); $formattedTree = $this->formatTree($tree); $users = Db::name('admin') ->field(['id', 'nickname']) ->select(); $this->view->assign('series',$formattedTree); $this->view->assign('users',$users); } public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model ->where($where) ->order($sort, $order) ->with([ 'contact' =>function ($q) { $q->field('id,nickname,mobile'); }, 'brand' =>function ($q) { $q->field('id,name'); }, 'series' =>function ($q) { $q->field('id,name'); }, ] ) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $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()->validate($validate); } $data = $this->getModelData($params); $data['created_at'] = now()->format('Y-m-d H:i:s'); $data['updated_at'] = now()->format('Y-m-d H:i:s'); $result = $this->model->allowField(true)->save($data); $this->dealExtend($params,$this->model->id); Db::commit(); } catch (ValidateException|PDOException|\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } 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) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $series_map = array_column($this->series,'name','id'); $series = $series_map[$row->series_id] ?? ''; $brand_map = array_column($this->brands,'name','id'); $brand = $brand_map[$row->brand_id] ?? ''; $series_name = $brand . ' / ' . $series; $row->series_name = $series_name; $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $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()->validate($validate); } $data = $this->getModelData($params); $data['updated_at'] = now()->format('Y-m-d H:i:s'); $result = $row->allowField(true)->save($data); $this->dealExtend($params,$row->id); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } public function extend(){ $data = \app\admin\model\car\Attributes::order('sort_order')->select(); $id = request()->get('id'); $value_map = []; if ($id){ $values = AttributeValue::where('car_id',$id)->select(); // dd($values); foreach ($values as $value){ $value_map[$value->attribute_id] = $value->value; } } $res = []; foreach ($data as $datum){ $datum->options = json_decode($datum->options); $re = $datum->toArray(); $out = [ 'id' => $re['id'], 'label' => $re['name'], 'type' => $re['input_type'], 'options' => $re['options'], 'name' => $re['field_key'], 'value' => $value_map[$re['id']] ?? null, ]; $res [] =$out; } $this->success(data:$res); } private function dealExtend(array $params,$car_id) { $data = \app\admin\model\car\Attributes::order('sort_order')->field('id,field_key')->select(); $insert = []; // dd($data); foreach ($data as $datum){ if (isset($params[$datum['field_key']])){ if (empty($params[$datum['field_key']])) continue; $insert [] = [ 'car_id' => $car_id, 'attribute_id' => $datum->id, 'value' => $params[$datum['field_key']], ]; } } // dd($insert); AttributeValue::where('car_id',$car_id)->delete(); AttributeValue::insertAll($insert); } private function getModelData(array $params) { $res = [ 'title' => $params['title'], 'price' => $params['price'], 'desc' => $params['desc'], 'count' => $params['count'], 're_price' => $params['re_price'], 'cover_image' => $params['cover_image'], 'contact_id' => $params['contact_id'] ?? $this->auth->id, ]; $series_id = $params['series_id']; if ($params['active_at']){ $res['active_at'] = $params['active_at']; } $series = \app\admin\model\Series::where('id',$series_id)->find(); $res ['series_id'] = $series_id; $res ['brand_id'] = $series->brand_id; if (!$res['contact_id'] || $res['contact_id'] == -1){ $res['contact_id'] = $this->auth->id; } return $res; } }