Installation:
composer require avro/csv-bundle
Enable in config/bundles.php:
Avro\CsvBundle\AvroCsvBundle::class => ['all' => true],
Avro\CaseBundle\AvroCaseBundle::class => ['all' => true],
Basic Configuration (config/packages/avro_csv.yaml):
avro_csv:
db_driver: orm
batch_size: 15
tmp_upload_dir: "%kernel.root_dir%/../web/uploads/tmp/"
sample_count: 5
objects:
user:
class: App\Entity\User
redirect_route: app_user_list
First Use Case:
<a href="{{ path('avro_csv_import_upload', {'alias': 'user'}) }}">Import Users</a>
/import/user to upload a CSV and map fields.Import Workflow:
batch_size) to avoid memory issues.redirect_route (e.g., a list view).Export Workflow:
/export/user to trigger a CSV download of all records.Event-Driven Customization:
RowAddedEvent to modify entities before persistence.
// src/EventListener/ImportListener.php
public function setDefaultValues(RowAddedEvent $event) {
$event->getObject()->setStatus('active');
}
AssociationFieldEvent.
// src/EventListener/AssociationListener.php
public function resolveUser(AssociationFieldEvent $event) {
$user = $this->em->getRepository(User::class)->findOneByEmail($event->getRow()['email']);
$event->getObject()->setOwner($user);
}
Bulk Operations:
batch_size to control memory usage (default: 15). Increase for large datasets (e.g., batch_size: 100).AvroCsvBundle\Form\Type\ImportType) to add custom validation.templates/AvroCsvBundle/ to customize UI.prePersist) for additional logic.ExporterInterface and ImporterInterface for unit tests:
$this->container->set('avro_csv.exporter', $this->createMock(ExporterInterface::class));
Memory Limits:
batch_size or use native DB imports for "gargantuan" files (per README).memory_limit in php.ini or optimize queries.Field Mapping:
@ImportExclude for sensitive fields (e.g., passwords).AvroCaseBundle (dependency) to normalize field names.Associations:
findOneBy on target entities. Custom logic may be needed for complex joins.$event->getRow() in AssociationFieldEvent to verify data.Temporary Files:
tmp_upload_dir. Ensure this directory is writable and cleaned up post-import.find /path/to/uploads/tmp/ -type f -mtime +7 -delete
Symfony 5+ Compatibility:
# config/services.yaml
Avro\CsvBundle\Service\Importer: ~
sample_count in config to ensure the preview shows correct data.debug:event-dispatcher).Custom Exporters:
Avro\CsvBundle\Export\Exporter to add support for non-Doctrine entities (e.g., Elasticsearch models).class ElasticExporter implements ExporterInterface {
public function getContent(): string {
// Custom logic to fetch from Elasticsearch
return $this->convertToCsv($data);
}
}
Validation Rules:
@Assert\Email) and handle failures in RowAddedEvent:
public function validateEmail(RowAddedEvent $event) {
$validator = $this->validator->validate($event->getObject());
if (count($validator)) {
throw new \RuntimeException('Validation failed: ' . (string) $validator);
}
}
Performance:
$cacheKey = 'export_user_' . md5($qb->getQuery()->getSQL());
if (!$csv = $this->cache->get($cacheKey)) {
$csv = $exporter->getContent();
$this->cache->set($cacheKey, $csv, 3600); // Cache for 1 hour
}
Security:
# config/routes/avro_csv.yaml
avro_csv_import_upload:
path: /import/{alias}
methods: [GET, POST]
defaults: { _controller: 'avro_csv.import', _role: 'ROLE_ADMIN' }
How can I help you explore Laravel packages today?