Issue Feature Closed

#102194Paginator for QueryBuilder

Philipp Kitzberger 10/18/23, 09:52 updated 03/31/26, 09:00

Description

Currently there’s two Paginators in the core:

  • \TYPO3\CMS\Core\Pagination\ArrayPaginator
  • \TYPO3\CMS\Extbase\Pagination\QueryResultPaginator

Neither of them is ideal for QueryBuilder results, right?

Sure we could use ArrayPaginator like this:

$query = $queryBuilder->select('*')->from('tt_content');
new ArrayPaginator($query->executeQuery()->fetchAll(), $currentPage, $itemsPerPage);

But this will always fire the whole SELECT statement without any LIMIT to the database.

I suggest to add a 3rd paginator to the core: QueryResultPaginator

Reviews 4

History 611

  1. Bastian Stargazer commented <span data-processor="relativetime" data-value="2023-12-28T13:43:38+00:00" > 12/28/23, 13:43 </span>

    Will this feature patch also be available in v11? That would be awesome!

  2. Stefan Bürk commented and changed status Under Review Rejected <span data-processor="relativetime" data-value="2025-01-09T08:56:10+00:00" > 01/09/25, 08:56 </span>

    Rejectiong this request as “Won’t do” as this is currently not doable in a working way,
    at least as a generic overall solution which TYPO3 should strive for.

    See comments in the provided change, but the current solution
    lacks support for two major things:

    • Full record language overlay handling
    • Full workspace support (moved,deleted,added in workspace, resorted, …)

    That means the plain QueryBuilder paginator could only be used in single language (default)
    instance without workspaces and is not reliable in instances using one or both and leading
    to a fallout of upcoming multipe issues targeting the missing support and asking why it
    does not automatically resolve these things.

    I don’t see any easy way to implement this in a satisfing way even with a lot of time investment
    plus the time required to deal with the following issue reports.

    I’d suggest to provide this paginator implementation as a dedicated extension mentioning the restriction.

  3. Christian Weiske commented <span data-processor="relativetime" data-value="2026-01-19T07:28:16+00:00" > 01/19/26, 07:28 </span>

    I’m using the following code since some time.
    And yes, it doesn’t support language overlays or workspaces.

    <?php
    
    declare(strict_types=1);
    
    namespace Vendor\Example\Helper;
    
    use TYPO3\CMS\Core\Database\Query\QueryBuilder;
    use TYPO3\CMS\Core\Pagination\AbstractPaginator;
    
    /**
     * Copy of TYPO3\CMS\Extbase\Pagination\QueryResultPaginator
     * that supports a QueryBuilder as source
     */
    class QueryBuilderPaginator extends AbstractPaginator
    {
        private QueryBuilder $queryBuilder;
    
        private array $paginatedQueryResult;
    
        public function __construct(
            QueryBuilder $queryBuilder,
            int $currentPageNumber = 1,
            int $itemsPerPage = 10
        ) {
            $this->queryBuilder = $queryBuilder;
            $this->setCurrentPageNumber($currentPageNumber);
            $this->setItemsPerPage($itemsPerPage);
    
            $this->updateInternalState();
        }
    
        public function getPaginatedItems(): iterable
        {
            return $this->paginatedQueryResult;
        }
    
        protected function updatePaginatedItems(int $limit, int $offset): void
        {
            $this->paginatedQueryResult = $this->queryBuilder
                ->setMaxResults($limit)
                ->setFirstResult($offset)
                ->execute()
                ->fetchAllAssociative();
        }
    
        protected function getTotalAmountOfItems(): int
        {
            return (clone $this->queryBuilder)->count('uid')->executeQuery()->fetchOne();
        }
    
        protected function getAmountOfItemsOnCurrentPage(): int
        {
            return count($this->paginatedQueryResult);
        }
    }
  4. Gerrit Code Review pushed patch set 7 for main to change 91373 status Rejected Under Review <span data-processor="relativetime" data-value="2026-03-06T06:46:10+00:00" > 03/06/26, 06:46 </span>

  5. Georg Ringer commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-03-06T12:40:08+00:00" > 03/06/26, 12:40 </span>

    Applied in changeset commit:dd36bc807a01d706f8780b8a502234d16919542f.