ecourty/doctrine-export-bundle
Installation:
composer require ecourty/doctrine-export-bundle
Enable the bundle in config/bundles.php if not using Symfony Flex.
First Export:
Use the ExportService to export entities to CSV:
use Ecourty\DoctrineExportBundle\Service\ExportService;
$exportService = $this->container->get(ExportService::class);
$entities = $entityManager->getRepository(Entity::class)->findAll();
$exportService->exportToFile($entities, 'csv', 'output.csv');
Quick Controller Example:
use Symfony\Component\HttpFoundation\Response;
use Ecourty\DoctrineExportBundle\Service\ExportService;
#[Route('/export', name: 'export')]
public function export(ExportService $exportService, EntityManagerInterface $em): Response
{
$entities = $em->getRepository(Entity::class)->findAll();
return $exportService->exportToBinaryResponse($entities, 'csv', 'export.csv');
}
Ecourty\DoctrineExportBundle\Service\ExportService (core functionality).config/packages/doctrine_export.yaml (if created).Ecourty\DoctrineExportBundle\Event\ExportEvents (for custom logic).Basic Export to File:
$exportService->exportToFile($entities, 'csv', 'path/to/file.csv');
Streaming Binary Response (for large datasets):
return $exportService->exportToBinaryResponse($entities, 'json', 'data.json');
Custom Field Mapping:
$exportService->exportToFile(
$entities,
'csv',
'output.csv',
['fields' => ['id', 'name', 'createdAt as date']]
);
Using Events for Pre/Post Processing:
// Subscribe to events in a listener
$dispatcher->addListener(
ExportEvents::PRE_EXPORT,
fn (ExportEvent $event) => $event->setData($event->getData() + ['custom_field' => true])
);
findBy() or createQueryBuilder() to filter entities before export.return new Response(
$exportService->exportToBinary($entities, 'csv'),
200,
['Content-Type' => 'text/csv', 'Content-Disposition' => 'attachment; filename="export.csv"']
);
Ecourty\DoctrineExportBundle\Processor\EntityProcessorInterface for complex transformations.exportToStream() for memory-efficient large exports:
$stream = $exportService->exportToStream($entities, 'csv');
file_put_contents('large_export.csv', $stream);
Memory Issues:
findBy() with pagination or exportToStream().findBy([], null, 1000) to fetch in chunks.Circular References:
User->Posts->User) may cause infinite loops. Use association_handling: 'ignore' in options:
$exportService->exportToFile($entities, 'json', 'output.json', ['association_handling' => 'ignore']);
Field Naming Conflicts:
field-name) may break CSV/JSON. Use fields option to alias:
['fields' => ['id', 'field-name as custom_field']]
Timezone Handling:
config/packages/doctrine_export.yaml:
doctrine_export:
default_timezone: 'UTC'
debug: true in config to log export events.ExportEvents::POST_EXPORT to inspect the final output:
$dispatcher->addListener(ExportEvents::POST_EXPORT, function (ExportEvent $event) {
error_log('Exported data:', $event->getData());
});
field_validation: true to skip invalid entities during export.Custom Formats:
Ecourty\DoctrineExportBundle\Writer\WriterInterface for new formats (e.g., Excel).Dynamic Field Selection:
fields option dynamically based on user input or roles:
$fields = $this->getFieldsBasedOnUserRole($user);
$exportService->exportToFile($entities, 'csv', 'output.csv', ['fields' => $fields]);
Post-Export Actions:
ExportEvents::POST_EXPORT to trigger notifications, archive files, or send emails:
$dispatcher->addListener(ExportEvents::POST_EXPORT, function (ExportEvent $event) {
$this->mailer->sendEmail('user@example.com', 'Export Ready', 'File attached', ['file' => $event->getFilePath()]);
});
Performance Tuning:
$entities = $em->getRepository(Entity::class)->findAll(); // No DTOs, use raw arrays
$exportService->exportToFile($entities, 'csv', 'output.csv', ['hydrate' => false]);
config/packages/doctrine_export.yaml:
doctrine_export:
default_format: 'csv'
default_options:
fields: ['id', 'name']
association_handling: 'ignore'
fields option are case-sensitive unless configured otherwise. Use field_mapping for case-insensitive aliases.How can I help you explore Laravel packages today?