Created
March 20, 2025 02:42
-
-
Save jpalala/abd79774be9d7b6a3325f256d4fbaeeb to your computer and use it in GitHub Desktop.
large file processor
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 | |
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