- range: documentation ( source)
<?php
function batchDelete(): \Generator
{
// $entityManager = $this->getEntityManager($class);
$queryResults = range(0, 102);
$i = 0;
$removedObjects = [];
foreach ($queryResults as $pos => $object) {
// $entityManager->remove($object[0]);
$removedObjects[] = $object;
if (0 === (++$i % 20)) {
yield from $removedObjects;
$removedObjects = [];
// $entityManager->flush();
// $entityManager->clear();
}
}
yield from $removedObjects;
// $entityManager->flush();
// $entityManager->clear();
}
function callerFunctionBatchDelete()
{
foreach (batchDelete() as $deletedObject) {
// Fire any event you may need regarding `$deletedObject`.
echo 'Some operation with object "'.$deletedObject."\".\n";
}
}
callerFunctionBatchDelete();