DataBundle
Installation
Install with composer:, (*1)
composer require j-ben87/data-bundle
Register the bundle in your app/AppKernel.php
:, (*2)
public function registerBundles()
{
$bundles = [
// ...
new JBen87\DataBundle\DataBundle(),
];
// ...
}
Configuration
The bundle exposes the following configuration:, (*3)
# app/config/config.yml
data:
culture: fr_FR # required - used to generate localized data with Faker
fixtures_dir: "%kernel.root_dir%/data/fixtures" # default value - directory where datasets fixtures files are located
datasets:
fake:
files:
- "user.yml"
- "address.yml"
processors: # optional - white list some processors (default to all if empty)
- "@app.data_fixtures.processor.user"
providers: # optional - white list some providers (default to all if empty)
other:
files:
- "..."
Basic usage
Command
The bundle provides a command similar to DoctrineFixturesBundle to load your fixtures using Alice and Faker., (*4)
Usage:
bin/console data:fixtures:load <dataset> [options]
Arguments:
dataset The dataset to load.
Options:
--append Append the data fixtures instead of deleting all data from the database first.
--purge-with-truncate Purge data by using a database-level TRUNCATE statement
Dataset
To load your fixtures, you first need to create a dataset., (*5)
A dataset is made of two things:, (*6)
- a directory containing
yml
fixtures files that will be loaded by Alice
- a
Dataset
service referencing the files to load (order matters)
Note: if you use configuration to define your datasets, the Dataset
service will be automatically handled for you., (*7)
Defintion
All you need to do is to list the fixtures files to load in the configuration in the order you want them to be processed., (*8)
# app/config/config.yml
data:
datasets:
fake:
files:
- "user.yml"
- "..."
Fixtures files
By default, the files containing the datasets fixtures are located in app/data/fixtures
but this can be configured., (*9)
# app/data/fixtures/fake/user.yml
AppBundle\Entity\User:
user_{1..10}:
firstname: <firstName()>
lastname: <lastName()>
email: <email()>
password: <password()>
That's it, you are ready to go!, (*10)
Advanced usage
Providers & Processors
Alice comes with Providers and [Processors][5]., (*11)
You can register yours with the command the same way you registered a Dataset
:, (*12)
- providers must be tagged with
data.provider
- processors must be tagged with
data.processor
services:
app.data_fixtures.provider.custom:
class: AppBundle\DataFixtures\Provider\CustomProvider
public: false
tags:
- { name: data.provider }
app.data_fixtures.processor.user:
class: AppBundle\DataFixtures\Processor\User
public: false
tags:
- { name: data.processor }
They will automatically be available and used to write your fixtures and process them., (*13)
Note: you can white list some providers or processors for a dataset in the configuration., (*14)
Datasets
If you can't or don't want to use configuration to define your datasets, you can also create them manually., (*15)
Create a Dataset
class somewhere in your project.
It must implement JBen87\DataBundle\Dataset\DatasetInterface
.
Alternatively it can also extend the base class JBen87\DataBundle\Dataset\Dataset
., (*16)
// src/AppBundle/DataFixtures/Dataset/FakeDataset.php
use JBen87\DataBundle\Dataset\Dataset;
class FakeDataset extends Dataset
{
/**
* @inheritDoc
*/
public function getFiles()
{
return [
'user.yml',
];
}
}
To be registered with the command, it must also be declared as a service with the tag data.dataset
.
Optional tag attribute alias
can be used to set the dataset name., (*17)
Important: if not provided, the dataset name is guessed from the service id (e.g. the name for the service app.data_fixtures.dataset.fake
will be fake
)., (*18)
services:
app.data_fixtures.dataset.fake:
class: AppBundle\DataFixtures\Dataset\FakeDataset
public: false
tags:
- { name: data.dataset }
Contributing
Pull requests are welcome., (*19)
Thanks to everyone who has contributed already., (*20)
Released under the MIT License, (*21)