Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.