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

Data Generator Bundle Laravel Package

akeneo-labs/data-generator-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require akeneo-labs/data-generator-bundle:~0.3
    

    Register the bundle in app/AppKernel.php:

    $bundles[] = new Pim\Bundle\DataGeneratorBundle\PimDataGeneratorBundle();
    
  2. Prerequisites: Ensure Akeneo PIM (v1.3–1.6) is installed with channels, locales, and currencies preconfigured.

  3. First Use Case: Generate a basic fixture configuration (config.yml) in Resources/config/:

    data_generator:
        output_dir: "%kernel.cache_dir%/fixtures"
        entities:
            attributes:
                count: 50
                identifier_attribute: "sku"
            families:
                count: 10
                attributes_count: 3
    

    Run the fixture generator:

    php app/console pim:generate:fixtures Resources/config/config.yml
    

Implementation Patterns

Workflow for High-Volume Testing

  1. Fixture Generation:

    • Use pim:generate:fixtures to create families, attributes, and categories (required for product generation).
    • Example: Generate 1,000 attributes with 50 families:
      entities:
          attributes:
              count: 1000
              identifier_attribute: "reference"
          families:
              count: 50
              attributes_count: 20
      
  2. Product Data Generation:

    • Generate CSV files for product imports using pim:generate:products-file.
    • Example: Create 5,000 products with forced values:
      entities:
          products:
              count: 5000
              mandatory_attributes: [sku, name, price]
              force_values: { brand: "TestBrand", category: "Electronics" }
      
  3. Integration with Akeneo:

    • Copy generated fixtures to Akeneo’s app/import/fixtures/ directory.
    • Use Akeneo’s import profiles to process the product CSV files.
  4. Automation:

    • Chain commands in a Makefile or CI script:
      # Generate fixtures → products → import
      make test-data
      
      test-data:
          php app/console pim:generate:fixtures config/fixtures.yml
          php app/console pim:generate:products-file config/products.yml
      

Advanced Patterns

  • Dynamic Configurations: Use environment variables or Symfony parameters for output_dir or count:

    output_dir: "%env(PIM_FIXTURES_DIR)%"
    entities:
        products:
            count: "%env(int:PRODUCT_COUNT, 1000)%"
    
  • Template Inheritance: Extend base configurations for reuse:

    # base.yml
    data_generator:
        output_dir: "%kernel.cache_dir%/fixtures"
        entities:
            attributes:
                identifier_attribute: "sku"
    
    # products.yml (extends base.yml)
    entities:
        products:
            <<: *base_config
            count: 2000
    
  • Post-Generation Hooks: Use Symfony’s EventDispatcher to process generated files (e.g., validate CSVs):

    // services.yml
    services:
        App\EventListener\DataGeneratorListener:
            tags:
                - { name: kernel.event_listener, event: pim.data_generator.post_generate, method: onPostGenerate }
    

Gotchas and Tips

Pitfalls

  1. Order Dependency:

    • Must run pim:generate:fixtures before pim:generate:products-file.
    • Products rely on pre-generated families/attributes. Skipping this causes errors like:
      [Error] Family "generated_family_1" not found.
      
  2. CSV Delimiters:

    • Default delimiter is ,. For non-US locales, override in config:
      entities:
          products:
              delimiter: ";"
      
  3. Memory Limits:

    • Generating >10,000 products may hit PHP memory limits. Increase memory_limit in php.ini or batch generation:
      # Generate in chunks
      entities:
          products:
              count: 10000
              batch_size: 1000  # Custom logic required (see "Extension Points")
      
  4. Faker Locale Mismatch:

    • If locales (e.g., fr_FR) aren’t set in Akeneo, Faker-generated text may fail validation. Pre-configure locales in Akeneo first.
  5. Overwritten Fixtures:

    • Regenerating fixtures overwrites existing files. Backup or use unique output_dir per run:
      output_dir: "%kernel.cache_dir%/fixtures_$(date +%s)"
      

Debugging Tips

  • Validate Config: Use Symfony’s debug:config to check loaded parameters:

    php app/console debug:config data_generator
    
  • Dry Run: Test with a small dataset first (e.g., count: 10) to verify structure.

  • Log Generation: Enable debug mode to log generated data:

    # config.yml
    monolog:
        handlers:
            data_generator:
                type: stream
                path: "%kernel.logs_dir%/data_generator.log"
                level: debug
    

Extension Points

  1. Custom Data Providers: Extend Pim\Bundle\DataGeneratorBundle\Generator\DataGenerator to inject custom logic:

    // src/Generator/CustomProductGenerator.php
    class CustomProductGenerator extends AbstractGenerator {
        protected function generateProduct() {
            // Override to add custom fields (e.g., "weight")
            $product = parent::generateProduct();
            $product['weight'] = rand(1, 100);
            return $product;
        }
    }
    
  2. Post-Processing: Use Symfony’s EventDispatcher to modify generated data:

    // src/EventListener/ProductGeneratorListener.php
    class ProductGeneratorListener {
        public function onGenerateProducts(ProductGenerateEvent $event) {
            foreach ($event->getProducts() as $product) {
                $product['custom_field'] = 'dynamic_value';
            }
        }
    }
    
  3. Batch Processing: Implement chunked generation for large datasets:

    // Custom command extending AbstractGeneratorCommand
    protected function execute(InputInterface $input, OutputInterface $output) {
        $count = $this->getCount();
        for ($i = 0; $i < $count; $i += $this->getBatchSize()) {
            $this->generateBatch($i, min($i + $this->getBatchSize(), $count));
        }
    }
    

Configuration Quirks

  • force_values vs. mandatory_attributes:

    • force_values overrides existing values (e.g., brand: "TestBrand").
    • mandatory_attributes ensures these fields are never empty (filled with random data).
  • Attribute Types:

    • The bundle supports standard types (text, number, etc.). For custom attribute types, extend the generator or pre-populate Akeneo with compatible types.
  • Category Handling:

    • Categories are generated but not assigned to products by default. Use categories_count to control how many categories a product belongs to:
      entities:
          products:
              categories_count: 3  # Assign 3 random categories per product
      
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor