Skip to content

Instantly share code, notes, and snippets.

@petronetto
Created August 12, 2020 14:40
Show Gist options
  • Save petronetto/e64e452ea3a2d660ae63ad658694c01d to your computer and use it in GitHub Desktop.
Save petronetto/e64e452ea3a2d660ae63ad658694c01d to your computer and use it in GitHub Desktop.
Dosctrine Generator
<?php declare(strict_types=1);
namespace My\Application\Repository;
class CustomerRepository extends Doctrine\ORM\EntityRepository
{
public function findAll()
{
$queryBuilder = $this->createQueryBuilder('c')
->select('c')
->orderBy('c.id');
$limit = 1000;
$offset = 0;
while (true) {
$queryBuilder->setFirstResult($offset);
$queryBuilder->setMaxResults($limit);
$customers = $queryBuilder->getQuery()->getResult();
if (count($customers) === 0) {
break;
}
foreach ($customers as $customer) {
yield $customer;
$this->_em->detach($customer);
}
$offset += $limit;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment