问题描述:
在使用包含group by语句的分页功能时,返回的查询结果总条数有误
问题原因:
select count(*) from ... group by ... 时, 返回结果为分组后的每组数据条数
框架默认会获取请求结果的第一条数据内容,所以只会得到 分组结果后的第一个组的数据条数
解决方法:
在 \Swoft\Db\Driver\Mysql\MysqlConnection::receive 新增以下带注释的语句
/**
* @return mixed
*/
public function receive()
{
$result = $this->connection->recv();
## 修复 group by 导致的总条数错误问题
if (is_array($result) && count(reset($result)) == 1 && @array_key_exists('count', reset($result)) && ($count = count($result)) > 1) {
unset($result);
$result[0] = ['count' => (string) $count];
}
···
}