model = new \app\admin\model\app\testpaper\Group; $this->testpaperModel = new \app\admin\model\app\testpaper\Testpaper; } /** * 列表 */ public function index() { //当前是否为关联查询 $this->relationSearch = false; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->where($where) ->order($sort, $order) ->paginate($limit); $rows = $list->items(); $ids = $this->model->field('id')->column('id'); $default = [ 'id'=>0, 'name'=>'未分组', 'testpaper_count'=>$this->testpaperModel->where(['group_id'=>['NOT IN',$ids]])->count() ]; array_unshift($rows,$default); $result = array("total" => $list->total(), "rows" => $rows,'group_ids'=>implode(",",$ids)); return $this->success("获取成功",$result); } /** * 删除题库分组 * @param $ids * @return void * @throws DbException * @throws DataNotFoundException * @throws ModelNotFoundException */ public function del($ids = null) { if (false === $this->request->isPost()) { $this->error(__("Invalid parameters")); } $ids = $ids ?: $this->request->post("ids"); if (empty($ids)) { $this->error(__('Parameter %s can not be empty', 'ids')); } $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $item) { $this->testpaperModel->where([ 'group_id'=>$item->id ])->update(['group_id'=>0]); $count += $item->delete(); } Db::commit(); } catch (PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success("操作成功"); } $this->error(__('No rows were deleted')); } }