pagination.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. Pagination
  2. ==========
  3. .. versionadded:: 2.2
  4. Starting with version 2.2 Doctrine ships with a Paginator for DQL queries. It
  5. has a very simple API and implements the SPL interfaces ``Countable`` and
  6. ``IteratorAggregate``.
  7. .. code-block:: php
  8. <?php
  9. use Doctrine\ORM\Tools\Pagination\Paginator;
  10. $dql = "SELECT p, c FROM BlogPost p JOIN p.comments c";
  11. $query = $entityManager->createQuery($dql)
  12. ->setFirstResult(0)
  13. ->setMaxResults(100);
  14. $paginator = new Paginator($query, $fetchJoinCollection = true);
  15. $c = count($paginator);
  16. foreach ($paginator as $post) {
  17. echo $post->getHeadline() . "\n";
  18. }
  19. Paginating Doctrine queries is not as simple as you might think in the
  20. beginning. If you have complex fetch-join scenarios with one-to-many or
  21. many-to-many associations using the "default" LIMIT functionality of database
  22. vendors is not sufficient to get the correct results.
  23. By default the pagination extension does the following steps to compute the
  24. correct result:
  25. - Perform a Count query using `DISTINCT` keyword.
  26. - Perform a Limit Subquery with `DISTINCT` to find all ids of the entity in from on the current page.
  27. - Perform a WHERE IN query to get all results for the current page.
  28. This behavior is only necessary if you actually fetch join a to-many
  29. collection. You can disable this behavior by setting the
  30. ``$fetchJoinCollection`` flag to ``false``; in that case only 2 instead of the 3 queries
  31. described are executed. We hope to automate the detection for this in
  32. the future.