Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Fixtures Generator Bundle Laravel Package

andreybolonin/doctrine-fixtures-generator-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Install the Bundle:

    composer require webonaute/doctrine-fixtures-generator-bundle
    

    Ensure your config/bundles.php includes:

    Webonaute\DoctrineFixturesGeneratorBundle\WebonauteDoctrineFixturesGeneratorBundle::class => ['all' => true],
    
  2. 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/.

  3. Key Files:

    • Resources/doc/index.md: Official documentation.
    • config/packages/doctrine_fixtures_generator.yaml: Bundle configuration.

First Use Case: Quick Fixture for Testing

  1. 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.

  2. Load the Fixture:

    php bin/console doctrine:fixtures:load
    

Implementation Patterns

Workflow: Snapshot-Based Fixtures

  1. Create a Snapshot:

    php bin/console doctrine-fixtures-generator:snapshot
    

    Generates a full set of fixtures (data/fixtures/snapshot/*.orm.xml) with:

    • Load order for relationships (e.g., User before Order).
    • References to linked entities (e.g., order.userUser#1).
  2. Customize Load Order:

    php bin/console doctrine-fixtures-generator:generate User 1 --load-order=10
    

    Explicitly sets the load order for User fixtures.


Integration Tips

  1. 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']]));
    
  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
    
  3. Exclude Entities: Configure in config/packages/doctrine_fixtures_generator.yaml:

    doctrine_fixtures_generator:
        excluded_entities: ['App\Entity\AuditLog']
    

Gotchas and Tips

Pitfalls

  1. Symfony Version Mismatch:

    • Use v1.x for Symfony 2.3–3.4.
    • Use v2.x for Symfony 4+.
    • Fix: Downgrade/upgrade the bundle version in composer.json.
  2. Circular References:

    • Snapshot mode may fail if entities have bidirectional relationships without proper load-order.
    • Fix: Manually adjust load orders or exclude problematic entities.
  3. ID Gaps:

    • If IDs are non-sequential (e.g., 1, 3, 5), the generator may miss records.
    • Fix: Use --range or specify all IDs explicitly.

Debugging

  1. Dry Run: Use --dry-run to preview generated fixtures without saving:

    php bin/console doctrine-fixtures-generator:generate User 1 --dry-run
    
  2. Log Output: Enable debug mode in config/packages/dev/doctrine_fixtures_generator.yaml:

    doctrine_fixtures_generator:
        debug: true
    

Extension Points

  1. 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']
    
  2. 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',
            ];
        }
    }
    

Tips

  1. Snapshot Optimization:

    • Exclude large tables (e.g., App\Entity\Log) from snapshots to reduce fixture size.
    • Use --exclude-entity in snapshot mode:
      php bin/console doctrine-fixtures-generator:snapshot --exclude-entity=App\Entity\Log
      
  2. 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
    
  3. CI/CD Integration: Automate fixture generation in pipelines:

    # .github/workflows/test.yml
    - name: Generate fixtures
      run: php bin/console doctrine-fixtures-generator:snapshot
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware