akeneo-labs/data-generator-bundle
Installation:
composer require akeneo-labs/data-generator-bundle:~0.3
Register the bundle in app/AppKernel.php:
$bundles[] = new Pim\Bundle\DataGeneratorBundle\PimDataGeneratorBundle();
Prerequisites: Ensure Akeneo PIM (v1.3–1.6) is installed with channels, locales, and currencies preconfigured.
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
Fixture Generation:
pim:generate:fixtures to create families, attributes, and categories (required for product generation).entities:
attributes:
count: 1000
identifier_attribute: "reference"
families:
count: 50
attributes_count: 20
Product Data Generation:
pim:generate:products-file.entities:
products:
count: 5000
mandatory_attributes: [sku, name, price]
force_values: { brand: "TestBrand", category: "Electronics" }
Integration with Akeneo:
app/import/fixtures/ directory.Automation:
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
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 }
Order Dependency:
pim:generate:fixtures before pim:generate:products-file.[Error] Family "generated_family_1" not found.
CSV Delimiters:
,. For non-US locales, override in config:
entities:
products:
delimiter: ";"
Memory Limits:
memory_limit in php.ini or batch generation:
# Generate in chunks
entities:
products:
count: 10000
batch_size: 1000 # Custom logic required (see "Extension Points")
Faker Locale Mismatch:
fr_FR) aren’t set in Akeneo, Faker-generated text may fail validation. Pre-configure locales in Akeneo first.Overwritten Fixtures:
output_dir per run:
output_dir: "%kernel.cache_dir%/fixtures_$(date +%s)"
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
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;
}
}
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';
}
}
}
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));
}
}
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:
Category Handling:
categories_count to control how many categories a product belongs to:
entities:
products:
categories_count: 3 # Assign 3 random categories per product
How can I help you explore Laravel packages today?