Neos\Media\Domain\Repository\ThumbnailRepository::iterate PHP Method

iterate() public method

This method is useful for batch processing huge result set as it clears the object manager and detaches the current object on each iteration.
public iterate ( Doctrine\ORM\Internal\Hydration\IterableResult $iterator, callable $callback = null ) : Generator
$iterator Doctrine\ORM\Internal\Hydration\IterableResult
$callback callable
return Generator
    public function iterate(IterableResult $iterator, callable $callback = null)
    {
        $iteration = 0;
        foreach ($iterator as $object) {
            $object = current($object);
            (yield $object);
            if ($callback !== null) {
                call_user_func($callback, $iteration, $object);
            }
            $iteration++;
        }
    }

Usage Example

 /**
  * Render ungenerated thumbnails
  *
  * Loops over ungenerated thumbnails and renders them. Optional ``limit`` parameter to limit the amount of
  * thumbnails to be rendered to avoid memory exhaustion.
  *
  * @param integer $limit Limit the amount of thumbnails to be rendered to avoid memory exhaustion
  * @return void
  */
 public function renderThumbnailsCommand($limit = null)
 {
     $thumbnailCount = $this->thumbnailRepository->countUngenerated();
     $iterator = $this->thumbnailRepository->findUngeneratedIterator();
     $this->output->progressStart($limit !== null && $thumbnailCount > $limit ? $limit : $thumbnailCount);
     $iteration = 0;
     foreach ($this->thumbnailRepository->iterate($iterator) as $thumbnail) {
         if ($thumbnail->getResource() === null) {
             $this->thumbnailService->refreshThumbnail($thumbnail);
             $this->persistenceManager->persistAll();
         }
         $this->output->progressAdvance(1);
         $iteration++;
         if ($iteration === $limit) {
             break;
         }
     }
 }