提交 d6e37dc5 编写于 作者: weixin_47267244's avatar weixin_47267244

支持全局设置分页转数组后的字段名

上级 ee97a11d
......@@ -713,12 +713,19 @@ Db::query()->limit(1);
### 分页查询带扩展字段
查询总记录数、总页数:
**查询总记录数、总页数:**
```php
$page = 1;
$count = 10;
$data = Db::query()->from('xxxtable')->paginate($page, $count);
// 指定转数组后的字段名
$data = TDb::query()->from('xxxtable')->paginate($page, $count, [
'field_list' => 'list',
'field_limit' => 'limit',
'field_total' => 'total',
'field_page_count' => 'page_count',
]);
$data->getList(); // 列表数据
$data->getTotal(); // 总记录数
......@@ -736,7 +743,7 @@ var_dump(json_encode($data)); // 支持序列化
]
```
不查询总记录数、总页数:
**不查询总记录数、总页数:**
```php
$page = 1;
......@@ -753,6 +760,19 @@ var_dump(json_encode($data)); // 支持序列化
]
```
**全局设置转数组后的字段名:**
配置`@app.db.paginate.fields`:
```php
[
'list' => 'list',
'limit' => 'limit',
'total' => 'total',
'pageCount' => 'page_count',
]
```
## 查询执行
### 查询记录
......
......@@ -580,12 +580,19 @@ $list = TestModel::query()->where('id', '=', 1)->select()->getArray();
### 分页查询带扩展字段
查询总记录数、总页数:
**查询总记录数、总页数:**
```php
$page = 1;
$count = 10;
$data = TestModel::query()->paginate($page, $count);
// 指定转数组后的字段名
$data = TestModel::query()->paginate($page, $count, [
'field_list' => 'list',
'field_limit' => 'limit',
'field_total' => 'total',
'field_page_count' => 'page_count',
]);
$data->getList(); // 列表数据
$data->getTotal(); // 总记录数
......@@ -603,7 +610,7 @@ var_dump(json_encode($data)); // 支持序列化
]
```
不查询总记录数、总页数:
**不查询总记录数、总页数:**
```php
$page = 1;
......@@ -620,6 +627,19 @@ var_dump(json_encode($data)); // 支持序列化
]
```
**全局设置转数组后的字段名:**
配置`@app.db.paginate.fields`:
```php
[
'list' => 'list',
'limit' => 'limit',
'total' => 'total',
'pageCount' => 'page_count',
]
```
### 聚合函数
```php
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Imi\Db\Query;
use Imi\Config;
use Imi\Db\Interfaces\IStatement;
use Imi\Db\Query\Interfaces\IPaginateResult;
use Imi\Db\Query\Interfaces\IResult;
......@@ -183,18 +184,19 @@ class PaginateResult implements IPaginateResult
if (null === $arrayData)
{
$options = $this->options;
$fields = Config::get('@app.db.paginate.fields', []);
$arrayData = [
// 数据列表
$options['field_list'] ?? 'list' => $this->getList(),
$options['field_list'] ?? ($fields['list'] ?? 'list') => $this->getList(),
// 每页记录数
$options['field_limit'] ?? 'limit' => $this->limit,
$options['field_limit'] ?? ($fields['limit'] ?? 'limit') => $this->limit,
];
if (null !== $this->total)
{
// 记录总数
$arrayData[$options['field_total'] ?? 'total'] = $this->total;
$arrayData[$options['field_total'] ?? ($fields['total'] ?? 'total')] = $this->total;
// 总页数
$arrayData[$options['field_page_count'] ?? 'page_count'] = $this->pageCount;
$arrayData[$options['field_page_count'] ?? ($fields['pageCount'] ?? 'page_count')] = $this->pageCount;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册