Installation:
composer require willdurand/faker-bundle
Add to config/bundles.php (Symfony) or AppKernel.php (legacy):
return [
// ...
WillDurand\BazingaFakerBundle\WillDurandBazingaFakerBundle::class => ['all' => true],
];
First Command:
Generate fake data for a model (e.g., User):
php bin/console bazinga:faker:load --entity=App\Entity\User --number=5
Flags:
--number: Number of records (default: 1).--force: Overwrite existing records.--dry-run: Preview SQL without execution.Where to Look First:
config/packages/bazinga_faker.yaml (if auto-configured).src/DataFixtures/ for custom fixture classes.# Load 10 fake users with emails and passwords (hashed)
php bin/console bazinga:faker:load --entity=App\Entity\User --number=10 --fields="email,password"
Note: Passwords require custom providers (see Implementation Patterns).
Basic Fixture Loading:
# config/packages/bazinga_faker.yaml
bazinga_faker:
services:
App\Entity\User:
fields:
- email
- name
providers:
- 'App\Faker\CustomUserProvider'
php bin/console bazinga:faker:load --entity=App\Entity\User
Custom Providers:
Extend Faker\Provider\Base for domain-specific data:
// src/Faker/CustomUserProvider.php
namespace App\Faker;
use Faker\Provider\Base;
class CustomUserProvider extends Base {
public function hashedPassword() {
return password_hash($this->generator->password, PASSWORD_DEFAULT);
}
}
Register in bazinga_faker.yaml:
providers:
App\Faker\CustomUserProvider::hashedPassword: password
Fixtures with Relationships:
Use bazinga:faker:load in a DataFixtures class:
// src/DataFixtures/UserFixtures.php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use WillDurand\BazingaBundle\Generator\Generator;
class UserFixtures extends Fixture {
public function load(ObjectManager $manager) {
$generator = new Generator();
$users = $generator->generateMany(
'App\Entity\User',
5,
['name', 'email']
);
$manager->persist($users);
$manager->flush();
}
}
Load via:
php bin/console doctrine:fixtures:load
Dynamic Field Mapping:
Use fields to map database columns to Faker methods:
fields:
email: email
slug: slug
created_at: dateTimeThisYear
Laravel Adaptation: Since this is a Symfony bundle, use laravel-faker instead for Laravel projects. Workaround: Manually integrate Faker via:
composer require fakerphp/faker
Then use Faker\Factory::create() directly in factories.
Testing:
Seed test databases in phpunit.xml:
<env name="FAKER_SEED" value="12345"/>
Use in tests:
$faker = Faker\Factory::create();
$faker->seed(12345); // Reproducible data
Performance: Batch inserts for large datasets:
$generator->generateMany('App\Entity\User', 1000, ['name', 'email']);
$manager->flush(); // Bulk insert
Symfony-Specific:
laravel-faker instead.Field Mapping Issues:
id, created_at, or updated_at may conflict with Faker methods.
Fix: Use custom providers or rename columns.Provider Conflicts:
bazinga_faker.yaml.App\Faker\CustomProvider).Dry-Run Debugging:
--dry-run to inspect SQL before execution:
php bin/console bazinga:faker:load --entity=User --dry-run
ON DUPLICATE KEY errors (use --force cautiously).Log Faker Output:
Enable debug mode in config/packages/dev/bazinga_faker.yaml:
debug: true
Logs will appear in var/log/dev.log.
Provider Debugging: Dump Faker generator state:
$generator = new \Faker\Generator();
dump($generator->format('{{user_name}}')); // Test provider output
Doctrine Events:
Listen for prePersist to modify fake data:
// src/EventListener/FakerListener.php
namespace App\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
class FakerListener implements EventSubscriber {
public function prePersist(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if ($entity instanceof \App\Entity\User) {
$entity->setPassword('default'); // Override fake password
}
}
// ...
}
Custom Generators:
Extend WillDurand\BazingaBundle\Generator\Generator for complex logic:
class CustomGenerator extends Generator {
protected function configure() {
$this->addProvider(new CustomUserProvider());
}
}
Register in services:
services:
App\Generator\CustomGenerator: ~
Dynamic Fixtures: Use environment variables to control fixture loading:
# .env
FAKER_LOAD_USERS=true
# src/DataFixtures/UserFixtures.php
if (getenv('FAKER_LOAD_USERS')) {
$this->load($manager);
}
Localization:
Override Faker locale in bazinga_faker.yaml:
locale: 'fr_FR' # French locale
Post-Processing: Use Doctrine lifecycle callbacks to transform fake data:
// App\Entity\User.php
use Doctrine\ORM\Mapping as ORM;
#[ORM\PrePersist]
public function prePersist() {
$this->email = strtolower($this->email);
}
Reproducible Data: Seed Faker with a fixed value for tests:
$faker = Faker\Factory::create();
$faker->seed(42); // Always returns the same data
Partial Updates:
Use --fields to update specific columns:
php bin/console bazinga:faker:load --entity=User --fields="email,status" --number=10
Composite Fields: Combine multiple Faker methods for complex data:
fields:
full_name: "{{user_name}} {{user_lastName}}"
bio: "{{realText}, {{lorem(paragraph)}}"
Exclude Fields: Skip certain fields during generation:
fields:
- id
- deleted_at
How can I help you explore Laravel packages today?