bluetea/import-bundle
Symfony2 bundle for importing CSV files and persisting data with Doctrine ORM. Extensible via factories for other file types and supports using a separate entity manager for import logic. Provides import functionality only (no views).
Installation:
composer require bluetea/import-bundle
Add to AppKernel.php:
new Bluetea\ImportBundle\ImportBundle(),
Configure:
Update config.yml with your import settings:
bluetea_import:
factories: ['%kernel.root_dir%/../src/AppBundle/Factory']
entity_manager: 'import_em' # Optional: Use a separate EM
First Use Case:
Create a CSV import factory (e.g., src/AppBundle/Factory/UserImportFactory.php):
namespace AppBundle\Factory;
use Bluetea\ImportBundle\Factory\AbstractFactory;
class UserImportFactory extends AbstractFactory
{
protected $entityClass = 'AppBundle\Entity\User';
protected $fields = ['username', 'email', 'role'];
}
Trigger Import: Use a controller or command to process a CSV:
use Bluetea\ImportBundle\Import\ImportManager;
$importManager = $this->get('bluetea_import.import_manager');
$importManager->import('user', $filePath);
Factory-Based Imports:
AbstractFactory for each entity type.processRow() to handle custom logic per row.public function processRow($row, $entity)
{
$entity->setPassword($this->encodePassword($row['password']));
}
Validation:
Validator in processRow() or validateRow().$errors = $this->validator->validate($entity);
if (count($errors)) {
$this->addError($row, $errors);
}
Bulk Operations:
bluetea_import:
flush_after_rows: 50 # Flush every 50 rows
Event-Driven:
import.pre_process, import.post_process, or import.error events:
$dispatcher->addListener('import.error', function ($event) {
$this->logger->error($event->getMessage());
});
prePersist/preUpdate for additional logic.monolog:
handlers:
import:
type: stream
path: "%kernel.logs_dir%/import.log"
level: debug
Deprecated Symfony2:
Memory Limits:
flush_after_rows or chunking:
$importManager->setFlushAfterRows(20);
Entity Manager Isolation:
import_em, ensure it’s cleared after imports:
$importManager->getEntityManager()->clear();
CSV Parsing Quirks:
,. Override in factory:
protected $delimiter = ';';
bluetea_import:
debug: true # Logs raw CSV data and errors
bin/console debug:event-dispatcher
Filter for import.* events.Custom Factories:
AbstractFactory and implementing supports():
public function supports($filePath)
{
return pathinfo($filePath, PATHINFO_EXTENSION) === 'xlsx';
}
Pre/Post Hooks:
preProcess() or postProcess() in factories for global logic.Custom Error Handling:
ImportErrorHandler to redirect errors to a queue or API:
class CustomErrorHandler extends ImportErrorHandler
{
public function handleError($error)
{
$this->apiClient->sendError($error);
}
}
Configuration Overrides:
services:
app.import_factory:
class: AppBundle\Factory\UserImportFactory
calls:
- [setValidator, ['@validator']]
How can I help you explore Laravel packages today?