Efficient iterator for Doctrine ORM. Allows for paginated traversing of database collection by specifing a QueryBuilder object that represents a query that retrieve the desired data. The only requirement is that the collection has to have a key with ability to order by it.
Each next query instead of using limit-offset query type is using the last item's key value to retrieve next resultset with keys greater then previous.
In SimpleIterator the hydration mode is always HYDRATE_OBJECT and the root entity needs to have 'id' field as its primary key.
$qb = $em->createQueryBuilder()->select('u')->from('User', 'u');
$iterator = new SimpleIterator($qb);
while ($result = $iterator->next()) {
foreach ($result as $item) {
// operate on single result items
}
$em->clear(); // here you can safely clear the entity manager
}
Iterator provides more configuration abilities then SimpleIterator. It allows to specify hydration mode and a function used to retrieve value of the key (useful for non-standard keys and/or hydration modes other than HYDRATE_OBJECT).
$qb = $em->createQueryBuilder()->select('u')->from('User', 'u');
$pullClosure = function($entity) { return $entity->getId(); };
$iterator = new Iterator();
$iterator->setQueryBuilder($qb);
$iterator->setIterateBy('u.id');
$iterator->setPullClosure($pullClosure);
while ($result = $iterator->next()) {
foreach ($result as $item) {
// operate on single result items
}
$em->clear(); // here you can safely clear the entity manager
}