model = model('SearchKeyword'); $this->view->assign("sexList", $this->model->getSexList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法 * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('pkey_name')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->with('book') ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->with('book') ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } public function add() { if ($this->request->isAjax()) { $ids = $this->request->get('ids'); $row = model('Book')->field('id,name,sex')->where('id', 'in', $ids)->select(); $params = []; $i =0; foreach ($row as $value) { $keywordexists = []; $keywordexists = model('SearchKeyword')->get(['keyword' => $value['name']]); if(empty($keywordexists)){ $params[$i]['keyword'] = $value['name']; $params[$i]['book_id'] = $value['id']; $params[$i]['sex'] = $value['sex']; $i = $i+1; } } model('SearchKeyword')->saveAll($params); $this->success('添加成功' .json_encode($ids)); } $this->error("非Ajax请求"); return $this->view->fetch(); } }