FastAdmin后台控制器的列表函数,对查询条件采用的是统一的处理方法,如果传递了某个查询条件,但不想让它用where处理而是用having,就需要在统一处理之前,对参数进行重新赋值。
示例代码如下:
//设置过滤方法
$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();
}
$filter = json_decode($this->request->get("filter"),true);
$op = json_decode($this->request->get("op"),true);
$having = "";
if(isset($filter['total_count'])){
$range = explode(',', $filter['total_count']);
$start = intval($range[0]);
$end = intval($range[1]);
$having = "total_count between {$start} and {$end}";
unset($filter['total_count']);
unset($op['total_count']);
}
$this->request->get(["filter"=>json_encode($filter),'op'=>json_encode($op)]);
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
上面出现的total_count
字段,实际不存在于表中,而是统计出来的,所以放在where
中会报错,需要放在having中。
所以在$this->buildparams()
之前,先构造好having
的查询条件,再把total_count
参数unset掉就好了。