/**
  * @param Query $pageQuery
  */
 protected function setPagerParameters(Query $pageQuery)
 {
     $pageQuery->setFirstResult($this->getFirstResult());
     $pageQuery->setMaxResults($this->bufferSize);
 }
Example #2
0
 /**
  * @param int
  * @param int
  * @return ResultSet
  */
 public function applyPaging($offset, $limit)
 {
     if ($this->query->getFirstResult() != $offset || $this->query->getMaxResults() != $limit) {
         $this->query->setFirstResult($offset);
         $this->query->setMaxResults($limit);
         $this->iterator = NULL;
     }
     return $this;
 }
 /**
  * Constructor.
  *
  * Stores various parameters that are otherwise unavailable
  * because Doctrine\ORM\Query\SqlWalker keeps everything private without
  * accessors.
  *
  * @param \Doctrine\ORM\Query              $query
  * @param \Doctrine\ORM\Query\ParserResult $parserResult
  * @param array                            $queryComponents
  */
 public function __construct($query, $parserResult, array $queryComponents)
 {
     $this->platform = $query->getEntityManager()->getConnection()->getDatabasePlatform();
     $this->rsm = $parserResult->getResultSetMapping();
     $this->queryComponents = $queryComponents;
     // Reset limit and offset
     $this->firstResult = $query->getFirstResult();
     $this->maxResults = $query->getMaxResults();
     $query->setFirstResult(null)->setMaxResults(null);
     parent::__construct($query, $parserResult, $queryComponents);
 }
 public function paginate($page, Query $query, $itemsPerPage = 10)
 {
     if (!is_numeric($page)) {
         throw new \Exception('Valor no numerico para la paginacion');
     }
     $this->currentPage = (int) $page;
     $this->itemsPerPage = $itemsPerPage;
     $this->calLastPage(count($query->getResult()));
     $this->calRange();
     $query->setFirstResult($this->offset())->setMaxResults($this->itemsPerPage);
     return $query->getResult();
 }
Example #5
0
 private function _getNextBatch()
 {
     if ($this->clearEntitiesOnBatch) {
         $em = $this->query->getEntityManager();
         foreach ($this->clearEntitiesOnBatch as $e) {
             $em->clear($e);
         }
     }
     if (!$this->_count || $this->_count == $this->resultsPerBatch) {
         if ($this->countTotal && $this->offset + $this->resultsPerBatch > $this->countTotal) {
             $maxResults = $this->countTotal - $this->offset;
         } else {
             $maxResults = $this->resultsPerBatch;
         }
         $this->results = $this->query->setFirstResult($this->offset)->setMaxResults($maxResults)->getResult($this->hydrationMode);
     } else {
         $this->results = array();
     }
     $this->offset += $this->resultsPerBatch;
     $this->_index = 0;
     $this->_count = count($this->results);
 }
 /**
  * @param Query|QueryBuilder $query
  * @param int                $page
  * @param int                $limit
  * @param Closure            $callback
  *
  * @return array
  */
 public function getPaginatedResult($query, $page, $limit, $callback = null)
 {
     $query->setFirstResult(($page - 1) * $limit)->setMaxResults($limit);
     if ($query instanceof QueryBuilder) {
         $query = $query->getQuery();
     }
     $items = $query->getResult();
     if ($callback == null) {
         $callback = function ($item) {
             return ['id' => $item->getId(), 'text' => (string) $item];
         };
     }
     $data = array_map($callback, $items);
     $more = count($data) >= $limit;
     return ['items' => $data, 'itemsPerPage' => $limit, 'more' => $more];
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function paginate(Query $query, $results, $offset, $arrayResult = true)
 {
     if ($results <= 0 || $results > $this->maxResults) {
         $results = $this->defaultResults;
     }
     if ($offset < 0) {
         $offset = 0;
     }
     $paginator = new Paginator($query, false);
     $query->setFirstResult($offset)->setMaxResults($results);
     if ($arrayResult) {
         $result = $query->getArrayResult();
     } else {
         $result = $query->getResult();
     }
     return ['recordsTotal' => count($paginator), 'recordsFiltered' => count($paginator), 'data' => $result];
 }
Example #8
0
 /**
  * Create and returns a doctrine pagination.
  *
  * @param Query $query the doctrine object query
  * @param int   $currentPageNumber current page
  * @param int   $itemCountPerPage items per page
  *
  * @return Paginator
  */
 public function getPaginator(Query $query, $currentPageNumber = null, $itemCountPerPage = null)
 {
     $this->setPagination($currentPageNumber, $itemCountPerPage);
     $query->setFirstResult($this->firstResult)->setMaxResults($this->maxResults);
     return new Paginator($query);
 }
 /**
  * Makes final preparation of a query object before its execute method will be called.
  *
  * @param Query $query
  */
 protected function prepareQueryToExecute(Query $query)
 {
     $query->setFirstResult($this->firstResult + $query->getMaxResults() * $this->page);
 }
Example #10
0
 /**
  * Creates offset.
  *
  * @access   public
  * @param    integer $iVal
  * @return   $this
  * @since    1.0.0-alpha
  * @version  1.0.0-alpha
  */
 public function offset($iVal)
 {
     $this->oQuery->setFirstResult($iVal);
     return $this;
 }
 /**
  * {@inheritdoc}
  */
 protected function prepareQueryToExecute(Query $query)
 {
     // always iterate from the first record
     $query->setFirstResult($this->firstResult);
 }
 /**
  * Calculate first result based on page and max-per-page
  */
 protected function calculateFirstResult()
 {
     $maxPerPage = $this->getMaxPerPage();
     $page = $this->getPage();
     $this->query->setFirstResult($maxPerPage * ($page - 1));
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function findCollectionSet($depth = 0, $filter = [], CollectionInterface $collection = null, $sortBy = [])
 {
     try {
         $dql = sprintf('SELECT n, collectionMeta, defaultMeta, collectionType, collectionParent, parentMeta, collectionChildren
              FROM %s AS n
                     LEFT OUTER JOIN n.meta AS collectionMeta
                     LEFT JOIN n.defaultMeta AS defaultMeta
                     LEFT JOIN n.type AS collectionType
                     LEFT JOIN n.parent AS collectionParent
                     LEFT JOIN n.children AS collectionChildren
                     LEFT JOIN collectionParent.meta AS parentMeta
              WHERE (n.depth <= :depth + :maxDepth OR collectionChildren.depth <= :maxDepthPlusOne)', $this->_entityName);
         if ($collection !== null) {
             $dql .= ' AND n.lft BETWEEN :lft AND :rgt AND n.id != :id';
         }
         if (array_key_exists('search', $filter) && $filter['search'] !== null) {
             $dql .= ' AND collectionMeta.title LIKE :search';
         }
         if (array_key_exists('locale', $filter)) {
             $dql .= ' AND (collectionMeta.locale = :locale OR defaultMeta != :locale)';
         }
         if ($sortBy !== null && is_array($sortBy) && sizeof($sortBy) > 0) {
             $orderBy = [];
             foreach ($sortBy as $column => $order) {
                 $orderBy[] = 'collectionMeta.' . $column . ' ' . (strtolower($order) === 'asc' ? 'ASC' : 'DESC');
             }
             $dql .= ' ORDER BY ' . implode(', ', $orderBy);
         }
         $query = new Query($this->_em);
         $query->setDQL($dql);
         $query->setParameter('maxDepth', intval($depth));
         $query->setParameter('maxDepthPlusOne', intval($depth) + 1);
         $query->setParameter('depth', $collection !== null ? $collection->getDepth() : 0);
         if ($collection !== null) {
             $query->setParameter('lft', $collection->getLft());
             $query->setParameter('rgt', $collection->getRgt());
             $query->setParameter('id', $collection->getId());
         }
         if (array_key_exists('search', $filter) && $filter['search'] !== null) {
             $query->setParameter('search', '%' . $filter['search'] . '%');
         }
         if (array_key_exists('limit', $filter)) {
             $query->setMaxResults($filter['limit']);
         }
         if (array_key_exists('offset', $filter)) {
             $query->setFirstResult($filter['offset']);
         }
         if (array_key_exists('locale', $filter)) {
             $query->setParameter('locale', $filter['locale']);
         }
         return new Paginator($query);
     } catch (NoResultException $ex) {
         return [];
     }
 }
Example #14
0
 /**
  * Given the Query it returns a new query that is a paginatable query
  * using a modified subselect.
  *
  * @param  Query $query
  * @return Query
  */
 public static function getPaginateQuery(Query $query, $offset, $itemCountPerPage, array $hints = array())
 {
     foreach ($hints as $name => $hint) {
         $query->setHint($name, $hint);
     }
     return $query->setFirstResult($offset)->setMaxResults($itemCountPerPage);
 }
 /**
  * Applies the paging configuration to the query object.
  *
  * @param Query        $query   Query object
  * @param QueryOptions $options Paging options
  */
 protected function applyPagingOptionsToQuery(Query $query, QueryOptions $options = null)
 {
     if ($options == null) {
         return;
     }
     if ($options->hasPagination()) {
         $query->setFirstResult(($options->page - 1) * $options->itemsPerPage);
         $query->setMaxResults($options->itemsPerPage);
     }
 }
Example #16
0
 /**
  * Returns an array of items for a page
  *
  * @param  integer        $offset           Page offset
  * @param  integer        $itemCountPerPage Number of items per page
  * @return \ArrayIterator
  */
 public function getItems($offset, $itemCountPerPage)
 {
     $this->query->setFirstResult($offset)->setMaxResults($itemCountPerPage);
     return $this->paginator->getIterator();
 }