model = new \app\admin\model\teacher\Task; $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList()); $this->assign('sim_sim_type', ConfigModel::getSimTypeList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ public function add() { if (false === $this->request->isPost()) { $this->assignConfig('rel_Ids', []); $this->assignConfig('rel_Names', []); $this->assignConfig('prel_Ids', []); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } if(empty($params['fault_id'])){ $this->error('请选择任务故障'); } if(count(explode(',',$params['fault_id']))!=3){ $this->error('故障部位必须为3个'); } $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { $fult_ids = $params['fault_id']; //是否采用模型验证 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); } $result = $this->model->allowField(true)->save($params); foreach(explode(',',$fult_ids) as $item){ if(!empty($item)){ $arr = [ 'task_id' => $this->model->task_id, 'fault_id' => $item, 'flag' => 1, 'update_time' => date('Y-m-d H:i:s'), ]; Db::name('task_fault')->insert($arr); } } 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')); } $where = [ 'sim_type' => $row->sim_type, 'fault_type' => 1, 'fault_state'=>0 ]; $selectData = Fault::where($where)->select(); foreach ($selectData as $key => $value){ $children = Fault::where(['parent_fault_id'=>$value['fault_id'],'fault_type' => 3,'fault_state'=>0])->select(); $selectData[$key]['children'] = $children; } unset($value); $selectData = collection($selectData)->toArray(); $row['selectData'] = $selectData; $row['fault_id_arr'] = !empty($row['fault_id']) ? explode(',',$row['fault_id']):''; if (false === $this->request->isPost()) { $this->view->assign('row', $row); $this->assignConfig('rel_Ids',explode(',',$row->fault_id)); $this->assignConfig('rel_Names',explode(',',$row->fault_name)); $this->assignConfig('prel_Ids',explode(',',$row->pfault_id)); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } if(empty($params['fault_id'])){ $this->error('请选择任务故障'); } if(count(explode(',',$params['fault_id']))!=3){ $this->error('故障部位必须为3个'); } $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { $fult_ids = $params['fault_id']; $result = $row->allowField(true)->save($params); foreach(explode(',',$fult_ids) as $item){ if(!empty($item)){ $isset = Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>$item])->find(); if(empty($isset)){ $arr = [ 'task_id' => $row->task_id, 'fault_id' => $item, 'flag' => 1, 'update_time' => date('Y-m-d H:i:s'), ]; Db::name('task_fault')->insert($arr); } } } //删除任务和故障关联表中 编辑后不存在的数据 Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>['not in',$params['fault_id']]])->delete(); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } //删除的时候,同步删除好几个表 }