andreybolonin/doctrine-fixtures-generator-bundle
Install the Bundle:
composer require webonaute/doctrine-fixtures-generator-bundle
Ensure your config/bundles.php includes:
Webonaute\DoctrineFixturesGeneratorBundle\WebonauteDoctrineFixturesGeneratorBundle::class => ['all' => true],
First Command:
Generate a fixture for an existing entity (e.g., User) with ID 1:
php bin/console doctrine-fixtures-generator:generate User 1
Outputs a .orm.xml fixture file in data/fixtures/.
Key Files:
Resources/doc/index.md: Official documentation.config/packages/doctrine_fixtures_generator.yaml: Bundle configuration.Generate a Fixture:
php bin/console doctrine-fixtures-generator:generate App\Entity\Product 1 2 3
Creates data/fixtures/Product.orm.xml with records for IDs 1, 2, and 3.
Load the Fixture:
php bin/console doctrine:fixtures:load
Create a Snapshot:
php bin/console doctrine-fixtures-generator:snapshot
Generates a full set of fixtures (data/fixtures/snapshot/*.orm.xml) with:
User before Order).order.user → User#1).Customize Load Order:
php bin/console doctrine-fixtures-generator:generate User 1 --load-order=10
Explicitly sets the load order for User fixtures.
Dynamic Fixture Generation: Use the bundle in a custom command to generate fixtures on demand:
use Webonaute\DoctrineFixturesGeneratorBundle\Command\GenerateCommand;
$command = new GenerateCommand();
$command->run(new ArrayInput(['entity' => 'App\Entity\Post', 'ids' => ['1', '2']]));
Partial Fixtures: Generate fixtures for a range of IDs (e.g., IDs 10–20):
php bin/console doctrine-fixtures-generator:generate App\Entity\Comment --range=10-20
Exclude Entities:
Configure in config/packages/doctrine_fixtures_generator.yaml:
doctrine_fixtures_generator:
excluded_entities: ['App\Entity\AuditLog']
Symfony Version Mismatch:
v1.x for Symfony 2.3–3.4.v2.x for Symfony 4+.composer.json.Circular References:
load-order.ID Gaps:
1, 3, 5), the generator may miss records.--range or specify all IDs explicitly.Dry Run:
Use --dry-run to preview generated fixtures without saving:
php bin/console doctrine-fixtures-generator:generate User 1 --dry-run
Log Output:
Enable debug mode in config/packages/dev/doctrine_fixtures_generator.yaml:
doctrine_fixtures_generator:
debug: true
Custom Fixture Format:
Override the default .orm.xml template by extending the bundle’s FixturesGenerator service:
# config/services.yaml
Webonaute\DoctrineFixturesGeneratorBundle\Service\FixturesGenerator:
class: App\Service\CustomFixturesGenerator
arguments: ['@doctrine.orm.entity_manager']
Pre/Post-Generation Hooks: Use Symfony’s event system to modify fixtures before/after generation:
// src/EventSubscriber/FixtureSubscriber.php
use Webonaute\DoctrineFixturesGeneratorBundle\Event\FixturesGenerateEvent;
class FixtureSubscriber implements EventSubscriber {
public static function getSubscribedEvents() {
return [
FixturesGenerateEvent::PRE_GENERATE => 'onPreGenerate',
FixturesGenerateEvent::POST_GENERATE => 'onPostGenerate',
];
}
}
Snapshot Optimization:
App\Entity\Log) from snapshots to reduce fixture size.--exclude-entity in snapshot mode:
php bin/console doctrine-fixtures-generator:snapshot --exclude-entity=App\Entity\Log
Fixture Versioning: Append a timestamp to fixture filenames to avoid conflicts:
php bin/console doctrine-fixtures-generator:generate User 1 --output=User_%timestamp%.orm.xml
CI/CD Integration: Automate fixture generation in pipelines:
# .github/workflows/test.yml
- name: Generate fixtures
run: php bin/console doctrine-fixtures-generator:snapshot
How can I help you explore Laravel packages today?