Created
March 13, 2018 09:12
-
-
Save bwaidelich/206c1ef506c49a902044b1d08a3c74d0 to your computer and use it in GitHub Desktop.
Neos Flow CLI Command to process entities in batches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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