Example #1
0
 /**
  * @param QueryBuilder $dql
  * @param null $limit
  * @param int $offset 0
  * @param null $page
  * @return array [
  *                  'total' => total number of records
  *                  'query' => QueryBuilder
  *              ]
  */
 public static function paginateResponse(QueryBuilder &$dql, $limit = null, $offset = 0, $page = null)
 {
     if ($page !== null) {
         $offset = $page !== null && $limit !== null ? $limit * ($page - 1) : null;
     }
     $clone = Pagination::cloneQuery($dql);
     $paginator = new Paginator($dql);
     $total = $paginator->count();
     if ($limit !== null && $offset !== null) {
         $dql->setFirstResult($offset)->setMaxResults($limit);
     }
     $fromAlias = current($dql->getRootAliases());
     $result = $dql->select("DISTINCT {$fromAlias}.id")->getQuery()->getArrayResult();
     $ids = array_map('current', $result);
     $clone->getParameters()->clear();
     if (count($ids) > 0) {
         $clone->where($clone->expr()->in("{$fromAlias}.id", $ids));
     } else {
         $clone->where("1=0");
     }
     return array('total' => $total, 'query' => $clone);
 }