This bundle integrates Faker, a PHP library that generates fake data for you. It provides a command to load random data for your model objects as simple as possible.
In real life, you'll have to populate your database with data. It's often a pain because it requires imagination and time you probably don't have or you don't want to waste. Faker to the rescue!
You just have to configure your entities to populate, then run the
faker:populate
command to add new data without effort. It's better than
fixtures because you don't need to write anything. If you drop your database,
then re create it and load fresh data.
Require willdurand/faker-bundle
to your composer.json
file:
{
"require": {
"willdurand/faker-bundle": "@stable"
}
}
Register the bundle in app/AppKernel.php
:
// app/AppKernel.php
public function registerBundles()
{
// ...
if (in_array($this->getEnvironment(), array('dev', 'test'))) {
// ...
$bundles[] = new Bazinga\Bundle\FakerBundle\BazingaFakerBundle();
}
}
Enable the bundle's configuration in app/config/config_dev.yml
:
# app/config/config_dev.yml
bazinga_faker: ~
In order to use the BazingaFakerBundle
, you have to configure it.
First of all if you use Doctrine or Mandango instead of Propel you must define it so that the bundle can reconfigure itself:
# app/config/config*.yml
bazinga_faker:
orm: doctrine
# app/config/config*.yml
bazinga_faker:
orm: mandango
Afterwards you just need to configure which entities you want to populate and in which quantity (default: 5).
# app/config/config*.yml
bazinga_faker:
entities:
Acme\LibraryBundle\Model\Author:
number: 5
Acme\LibraryBundle\Model\Book:
number: 5
custom_formatters:
Isbn: { method: lexify, parameters: [ '?????????????' ] }
You can add your own formatter for each column of each entity:
bazinga_faker:
entities:
Acme\LibraryBundle\Model\Book:
custom_formatters:
Isbn: { method: randomElement, parameters: [ [ 'aaaaaaaaaa', 'bbbbbbbb', 'cccccccc' ] ] }
You can use all formatters provided by Faker, with or without arguments:
bazinga_faker:
entities:
Acme\LibraryBundle\Model\Book:
custom_formatters:
Isbn: { method: word }
You can also set null
to a column value in order to get the default value
generated by a behavior (e.g. Propel behaviors):
bazinga_faker:
entities:
Acme\LibraryBundle\Model\Book:
custom_formatters:
Slug: { method: null }
There are a few more optional settings available for more advanced customization of faker:
bazinga_faker:
seed: 1234
locale: en_GB
populator: Your\Own\Populator
entity: Your\Own\EntityPopulator
Adding additional providers to the generator is as easy as registering a tagged service to the container:
services:
faker.provider.my_customer_provider:
class: Acme\LibraryBundle\Faker\MyCustomerProvider
arguments: [@faker.generator]
tags:
- { name: bazinga_faker.provider }
The bundle provides a new Symfony2 command: faker:populate
which will populate
all configured entities.
php app/console faker:populate