// Usually in a controller $queryBuilder = $this->itemRepository->queryBuilderForFindAll(); $results = $paginationService->paginate( $queryBuilder, $currentPageNumber, $itemsPerPage );
This is necessary because we need to get the result for the page as well as the number of total results (with a database it would mean two queries). Unfortunately this kind of logic tends to leak in our controllers which is why Benjamin Eberlei suggests an elegant alternative:
// Can be done in a service $result = $this->itemRepository->findAll(); // Can be done in a ViewListener $page = $result->take($currentPageNumber, $itemsPerPage);
QueryBuilder is actually wrapped in a
Result which provides the possibility
to get all of them or a single portion.
take method returns the
PaginatorService wrapped in a
Page which contains
all the meta data we usually expect (page number, total elements, etc).
To make it possible he created porpaginas,
a small library that provides
Page as interfaces.
The actual pagination logic is delegated to the library of our choice, using adapters.