Installation
composer require aquis/xporter-bundle:*@dev
Ensure your config/bundles.php includes:
return [
// ...
Aquis\XporterBundle\XporterBundle::class => ['all' => true],
];
First Export
Use the CLI command to export a single entity (e.g., User) to YAML:
php bin/console xporter:export User
Output: var/exports/User_[timestamp].yml
First Import Import the generated YAML back into the database:
php bin/console xporter:import var/exports/User_[timestamp].yml
vendor/aquis/xporter-bundle/src/Command/ for ExportCommand and ImportCommand.config/packages/xporter.yaml (if auto-generated) for customization options.Export Workflow
php bin/console xporter:export User --limit=10 --where="active=1"
--with-relations to include associated entities (e.g., User with Address):
php bin/console xporter:export User --with-relations=address
--output-dir=/custom/path.Import Workflow
--append to avoid conflicts:
php bin/console xporter:import fixtures/user.yml --append
--validate to check YAML syntax before import.Integration with Tests
php bin/console xporter:export User --env=test > tests/fixtures/users.yml
phpunit.xml:
<env name="KERNEL_CLASS" value="App\Kernel"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
<env name="APP_ENV" value="test"/>
<env name="APP_DEBUG" value="true"/>
<env name="XPORTER_IMPORT_FILE" value="%kernel.project_dir%/tests/fixtures/users.yml"/>
Custom Exporters: Extend the bundle by creating a custom exporter service:
// src/Service/CustomExporter.php
class CustomExporter implements ExporterInterface {
public function export(EntityManager $em, string $entityClass, array $options): string {
// Custom logic (e.g., transform data before export)
return $yamlContent;
}
}
Register it in services.yaml:
services:
App\Service\CustomExporter:
tags: ['xporter.exporter']
Pre/Post-Import Hooks: Use Symfony events to modify data before/after import:
// config/services.yaml
App\EventListener\ImportListener:
tags:
- { name: kernel.event_listener, event: xporter.import.pre, method: onPreImport }
Circular References
User ↔ Role) may cause infinite loops.--with-relations sparingly or manually break cycles in YAML.Large Datasets
--limit or optimize YAML serialization.Doctrine Events
$em->getEventManager()->clear();
Unique Constraints
email in User) will fail silently or throw exceptions.--append or validate YAML first.--dry-run to preview changes without executing:
php bin/console xporter:import user.yml --dry-run
# config/packages/xporter.yaml
xporter:
debug: true
symfony/validator to validate fixtures before import:
use Symfony\Component\Validator\Validator\ValidatorInterface;
$validator = $container->get(ValidatorInterface::class);
$errors = $validator->validate($yamlData);
Custom Fixture Format
Override the default YAML writer by implementing FixtureWriterInterface:
class JsonFixtureWriter implements FixtureWriterInterface {
public function write(array $data, string $path): void {
file_put_contents($path, json_encode($data));
}
}
Register it in services.yaml:
App\Service\JsonFixtureWriter:
tags: ['xporter.fixture_writer']
Dynamic Export Filters
Use a custom query builder by extending ExportCommand:
protected function getQueryBuilder(string $entityClass): QueryBuilder {
$qb = parent::getQueryBuilder($entityClass);
$qb->andWhere('u.createdAt > :date')
->setParameter('date', new \DateTime('-7 days'));
return $qb;
}
Environment-Specific Fixtures Use Symfony’s parameter system to switch fixtures per environment:
# config/packages/dev/xporter.yaml
xporter:
import_files:
- "%kernel.project_dir%/config/fixtures/dev/users.yml"
How can I help you explore Laravel packages today?