美文网首页
ThinkPHP5分页问题

ThinkPHP5分页问题

作者: 猪猪晶 | 来源:发表于2017-04-18 13:22 被阅读0次
/**
  * THinkPHP5使用group by分页出现异常,无法正常使用
  * 类似$model->group('area_id')->paginate(2)的分页出错,
  * 修改\Thinkphp\library\think\db\Query.php的count()方法如下:
  * 修复group情况下count不正确
  * @author lifee
  */
public function count($field = '*')
{
    $options = $this->getOptions();
    if($options['group']) {
        $bind  = $this->bind;
        //构建lists sql
        $lists = $this->options($options)->field('count(*)')->bind($bind)->fetchSql(true)->select();
        //构建子查询统计sql
        $sql = "select count(*) as tp_count from ($lists) a";
        $resultSet = $this->query($sql, $bind);
        return $resultSet[0]['tp_count'];
    }
    return $this->value('COUNT(' . $field . ') AS tp_count', 0);
}

相关文章

网友评论

      本文标题:ThinkPHP5分页问题

      本文链接:https://www.haomeiwen.com/subject/mpvfzttx.html