think-orm
think-orm copied to clipboard
分页功能的小bug
Bug描述
BaseQuery的分页代码如下:
public function paginate($listRows = null, $simple = false): Paginator
{
if (is_int($simple)) {
$total = $simple;
$simple = false;
}
$defaultConfig = [
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'list_rows' => 15, //每页数量
];
if (is_array($listRows)) {
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
} else {
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
}
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = $page < 1 ? 1 : $page;
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();
if (!isset($total) && !$simple) {
$options = $this->getOptions();
unset($this->options['order'], $this->options['limit'], $this->options['page'], $this->options['field']);
$bind = $this->bind;
$total = $this->count();
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
}
$this->removeOption('limit');
$this->removeOption('page');
return Paginator::make($results, $listRows, $page, $total, $simple, $config);
}
如果在TP框架中,自定义了分页类bind('think\Paginator',OtherPaginator::class)后,orm的分页类却还是按照原始分页类来实现,可能会导致orm分页无法获取自定义分页中覆盖掉的分页配置。
现版本解决办法(<=2.0.36)
在框架的database.php自定义query类:
connections.[default].query=>YourBaseQuery::class
在YourBaseQuery::class重写paginate方法:
public function paginate($listRows = null, $simple = false): Paginator
{
$defaultConfig = [
'var_page' => 'current', //自定义的请求分页参数
'list_rows' => is_int($listRows) ? $listRows : 10, //自定义默认的每页条数
];
return parent::paginate($defaultConfig, $simple); // TODO: Change the autogenerated stub
}
你是在哪里bind的?
在系统服务的register中使用$this->app->bind('think\Paginator',HuikePaginator::class);