j-ben87/data-bundle

Allow to load fixtures datasets through command line.

Installs: 4 458

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 1

Open Issues: 0

Type:symfony-bundle

1.2.1 2018-01-24 17:24 UTC

This package is auto-updated.

Last update: 2024-11-06 07:46:23 UTC


README

Installation

Install with composer:

composer require j-ben87/data-bundle

Register the bundle in your app/AppKernel.php:

public function registerBundles()
{
    $bundles = [
        // ...
        new JBen87\DataBundle\DataBundle(),
    ];

    // ...
}

Configuration

The bundle exposes the following configuration:

# 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.

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.

A dataset is made of two things:

  • 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.

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.

# 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.

# 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!

Advanced usage

Providers & Processors

Alice comes with Providers and Processors.

You can register yours with the command the same way you registered a Dataset:

  • 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.

Note: you can white list some providers or processors for a dataset in the configuration.

Datasets

If you can't or don't want to use configuration to define your datasets, you can also create them manually.

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.

// 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.

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).

services:
    app.data_fixtures.dataset.fake:
        class: AppBundle\DataFixtures\Dataset\FakeDataset
        public: false
        tags:
            - { name: data.dataset }

Contributing

Pull requests are welcome.

Thanks to everyone who has contributed already.

Released under the MIT License