Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created March 13, 2018 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bwaidelich/206c1ef506c49a902044b1d08a3c74d0 to your computer and use it in GitHub Desktop.
Save bwaidelich/206c1ef506c49a902044b1d08a3c74d0 to your computer and use it in GitHub Desktop.
Neos Flow CLI Command to process entities in batches
<?php
declare(strict_types=1);
namespace Some\Package\Command;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
class SomeCommand extends CommandController
{
/**
* @Flow\InjectConfiguration(package="Neos.Flow")
* @var array
*/
protected $flowSettings;
/**
* @Flow\Inject
* @var SomeRepository
*/
protected $someRepository;
/**
* Some description
*
* @param int $batchSize number of entities to process at once
*/
public function processAllCommand(int $batchSize = 50)
{
$entityCount = $this->someRepository->countAll();
$this->outputLine('Processing <b>%s</b> entities.', [$entityCount]);
$this->output->progressStart($documentCount);
$offset = 0;
while ($offset < $documentCount) {
$commandArguments = [
'offset' => $offset,
'limit' => $batchSize
];
Scripts::executeCommand('some.package:some:processbatch', $this->flowSettings, true, ['offset' => $offset, 'limit' => $batchSize]);
$offset = min($offset + $batchSize, $entityCount);
$this->output->progressSet($offset);
}
$this->output->progressFinish();
$this->outputLine('<success>Done.</success>');
}
/**
* @param int $offset
* @param int $limit
* @internal
*/
public function processBatchCommand(int $offset, int $limit)
{
$allEntities = $this->someRepository->findAll();
$query = $allEntities->getQuery();
$query->setOffset($offset);
$query->setLimit($limit);
/** @var SomeEntity $entity */
foreach ($query->execute() as $entity) {
// do something with $entity
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment