Installation
Run composer require clickandmortar/import-bundle and enable the bundle in AppKernel.php (Laravel 5.5+ users should register it in config/app.php under providers).
Basic Configuration
Define a minimal config/import.php (or config.yml in Symfony) with:
click_and_mortar_import:
entities:
user_import:
model: App\User
unique_key: email
mappings:
name: "FullName"
email: "EmailAddress"
First Use Case
Upload a CSV file (e.g., users.csv) with headers matching your mappings. Trigger the import via:
use ClickAndMortar\ImportBundle\ImportManager;
$importManager = $this->get('click_and_mortar_import.manager');
$result = $importManager->import('user_import', 'path/to/users.csv');
Batch Processing
For large files, chunk the import using Laravel’s Chunk helper or Symfony’s BatchProcessor:
$importManager->import('user_import', $filePath, ['chunk_size' => 100]);
Pre/Post-Import Hooks
Extend the bundle by overriding the ImportManager service:
services:
click_and_mortar_import.manager:
class: App\Services\CustomImportManager
arguments: ['@click_and_mortar_import.manager']
Implement preImport() and postImport() in CustomImportManager.
Dynamic Mappings Use a service to generate mappings at runtime (e.g., for API imports):
$mappings = $this->generateDynamicMappings($apiResponse);
$importManager->import('dynamic_entity', $filePath, ['mappings' => $mappings]);
FormRequest or Symfony’s Validator to validate imported data before saving.Log facade or Symfony’s Monolog to track import progress:
$importManager->import('user_import', $filePath, ['log' => true]);
dispatch(new ImportJob('user_import', $filePath));
Deprecated Symfony Bundle
The package is designed for Symfony 2/3. For Laravel, wrap it in a service layer or use a Laravel-compatible alternative like maatwebsite/excel.
Unique Key Conflicts
Ensure unique_key in config matches a database column. Skipped rows will log warnings but not fail.
File Encoding Issues Specify encoding in the import options:
$importManager->import('user_import', $filePath, ['encoding' => 'UTF-8']);
Case-Sensitive Headers
CSV headers must match mappings exactly (case-sensitive). Use strtolower() in mappings if needed:
mappings:
name: "fullname" # Matches "FullName" in CSV
$importManager->import('user_import', $filePath, ['dry_run' => true]);
click_and_mortar_import:
debug: true
ImportManager event system (Symfony) or Laravel’s after hooks.Custom Writers
Override the Writer class to support new formats (e.g., JSON):
class JsonWriter extends AbstractWriter
{
public function write(array $data) { /* ... */ }
}
Register it in config:
click_and_mortar_import:
writers:
json: App\Services\JsonWriter
Event Listeners
Listen for import.start/import.end events (Symfony) or Laravel’s importing/imported events to trigger actions.
Database Transactions Wrap imports in transactions for atomicity:
DB::transaction(function () use ($importManager) {
$importManager->import('user_import', $filePath);
});
How can I help you explore Laravel packages today?