for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Zenstruck\Porpaginas\Doctrine;
use Doctrine\DBAL\Query\QueryBuilder;
use Zenstruck\Porpaginas\Callback\CallbackPage;
use Zenstruck\Porpaginas\Result;
/**
* @author Kevin Bond <[email protected]>
*/
final class DBALQueryBuilderResult implements Result
{
private $qb;
private $countQueryBuilderModifier;
private $count;
* @param QueryBuilder $qb
* @param callable|null $countQueryBuilderModifier
public function __construct(QueryBuilder $qb, callable $countQueryBuilderModifier = null)
$this->qb = $qb;
$this->countQueryBuilderModifier = $countQueryBuilderModifier ?: function (QueryBuilder $qb) {
return $qb->select('COUNT(*)');
};
}
* {@inheritdoc}
public function take($offset, $limit)
$qb = clone $this->qb;
$results = function ($offset, $limit) use ($qb) {
return $qb
->setFirstResult($offset)
->setMaxResults($limit)
->execute()
->fetchAll();
return new CallbackPage($results, [$this, 'count'], $offset, $limit);
public function count()
if ($this->count !== null) {
return $this->count;
call_user_func($this->countQueryBuilderModifier, $qb);
return $this->count = (int) $qb->execute()->fetchColumn();
public function getIterator()
$stmt = $this->qb->execute();
while ($data = $stmt->fetch()) {
yield $data;