Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jpalala/abd79774be9d7b6a3325f256d4fbaeeb to your computer and use it in GitHub Desktop.
Save jpalala/abd79774be9d7b6a3325f256d4fbaeeb to your computer and use it in GitHub Desktop.
large file processor
<?php
use Doctrine\ORM\EntityManagerInterface;
use DoctrineBatchUtils\BatchProcessing\SimpleBatchIteratorAggregate;
class LargeFileProcessor
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public function processDatabase()
{
$qb = $this->entityManager->createQueryBuilder();
$qb->select('e')
->from('App\Entity\YourEntity', 'e');
$iterator = SimpleBatchIteratorAggregate::fromQuery(
$qb->getQuery(),
$this->entityManager,
1000 // Batch size
);
foreach ($iterator as $entity) {
// Process each entity
echo "Processing: " . $entity->getValue() . "\n";
}
}
}
✅ Advantages
Uses Doctrine's batch iterator to prevent memory issues.
Automatic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment