<?php
use PHPSnippets\Db\Entity\Test;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\Tools\Pagination\Paginator;
function paginate(QueryBuilder $qb, $firstResult, $maxResult)
{
$qb->setFirstResult((int) $firstResult)
->setMaxResults((int) $maxResult);
$paginator = new Paginator($qb->getQuery(), true);
return $paginator;
}
$qb = $entityManager->createQueryBuilder();
$qb->select('t')
->from(Test::CLASS, 't');
foreach (paginate($qb, 0, 2) as $entity) {
echo $entity->id . ', ' . $entity->name . PHP_EOL;
}