Skip to content

Instantly share code, notes, and snippets.

@zhouyl
Created July 18, 2014 02:34
Show Gist options
  • Select an option

  • Save zhouyl/40f4979eca333e4c77f5 to your computer and use it in GitHub Desktop.

Select an option

Save zhouyl/40f4979eca333e4c77f5 to your computer and use it in GitHub Desktop.
Fixed Phalcon\Paginator\Adapter\QueryBuilder bug
<?php
namespace Base\Paginator;
class QueryBuilder extends \Phalcon\Paginator\Adapter\QueryBuilder
{
public function getPaginate()
{
// 解决 php5.3 在空记录集时,调用 getPaginate() 方法出错的 bug
// @link http://forum.phalconphp.com/discussion/2279/phalcon-paginator-adapter-querybuilder-getpaginate-trying-to-get
try {
$paginate = parent::getPaginate();
} catch (\ErrorException $e) {
$paginate = new \stdClass;
$paginate->items = array();
$paginate->current = 1;
$paginate->before = 1;
$paginate->first = 1;
$paginate->next = 0;
$paginate->last = 0;
$paginate->total_pages = 0;
$paginate->total_items = 0;
}
$builder = $this->getQueryBuilder();
// 解决 query builder 中存在 group by 时的统计不准确的 bug
// @link https://github.com/phalcon/cphalcon/issues/2411
if ($group = $builder->getGroupBy()) {
$result = $builder->columns('COUNT(DISTINCT ' . $group . ') as TOTAL')
->groupBy(null)
->getQuery()
->execute()
->getFirst();
$paginate->total_items = $result->total;
$paginate->total_pages = ceil($paginate->total_items / $this->getLimit());
}
return $paginate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment