Buy

Delightful Dummy Data with Alice

Now things are about to get fun. A few minutes ago, we installed a library called nelmio/alice - search for that and find their GitHub page.

In a nutshell, this library lets us add fixtures data via YAML files. It has an expressive syntax and it ships with a bunch of built-in functions for generating random data. Actually, it uses yet another library behind the scenes called Faker to do that. It's the PHP circle of life!

Creating the Fixture YAML File

Find the ORM directory and create a new file called - how about fixtures.yml. That filename lacks excitement, but at least it's clear.

Start with the class name you want to create - AppBundle\Entity\Genus. Next, each genus needs an internal, unique name - it could be anything. But wait! Finish the name with 1..10:

7 lines src/AppBundle/DataFixtures/ORM/fixtures.yml
AppBundle\Entity\Genus:
genus_{1..10}:
... lines 3 - 7

With this syntax, Alice will loop over and create 10 Genus objects for free. Boom!

To finish things, set values on each of the Genus properties: name: <name()>. You could just put any value here, but when use <>, you're calling a built-in Faker function. Next, use subFamily: <text(20)> to generate 20 characters of random text, speciesCount: <numberBetween(100, 100000)> and funFact: <sentence()>:

7 lines src/AppBundle/DataFixtures/ORM/fixtures.yml
AppBundle\Entity\Genus:
genus_{1..10}:
name: <name()>
subFamily: <text(20)>
speciesCount: <numberBetween(100, 100000)>
funFact: <sentence()>

That's it team! To load this file, open up LoadFixtures and remove all of that boring garbage. Replace it with Fixtures - autocomplete that to get the use statement - then ::load(). Pass this __DIR__.'/fixtures.yml' and then the entity manager:

16 lines src/AppBundle/DataFixtures/ORM/LoadFixtures.php
... lines 1 - 7
use Nelmio\Alice\Fixtures;
class LoadFixtures implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$objects = Fixtures::load(__DIR__.'/fixtures.yml', $manager);
}
}

Now, run the exact command as before:

./bin/console doctrine:fixtures:load

I love when there are no errors. Refresh the list page. Voila: 10 completely random genuses. I love Alice.

All The Faker Functions

Well.... the genus name is actually the name of a person... which is pretty ridiculous. Let's fix that in a second.

But first, Nelmio's documentation has a ton of cool examples of things you can do with this library. But the biggest things you'll want to check out is the Faker library that this integrates. This shows you all of the built-in functions we were just using - like numberBetween, word, sentence and a ton more. There is some great stuff in here.

Now if we can just make the genus name a little more realistic.

Leave a comment!

  • 2016-09-27 Victor Bocharsky

    Hey Maksym,

    Thanks for sharing your steps!

    What about locale: first of all, try to use `ru_RU` instead of `ru` on `hautelook_alice.locale` key because this locale uses by Faker. Then, I think you should activate translator in your app/config.yml:

    framework:
    translator: { fallback: en }

    Also double check that the Faker's features you use is properly localized here: https://github.com/fzaninotto/... - otherwise there's no any effect, it'll fallback to EN.

    Cheers!

  • 2016-09-27 Maksym Minenko

    OMG, a couple of hours of frustration and now I've found the solution myself... I just had to use hautelook_alice:doctrine:fixtures:load

    Ok, maybe this will be helpful to somebody, so the steps are:
    1) composer require --dev hautelook/alice-bundle doctrine/data-fixtures
    2) activate it in the AppKernel:
    $bundles[] = new Hautelook\AliceBundle\HautelookAliceBundle();
    3) create an yml file in the src/AppBundle/DataFixtures/ORM folder (we don't need any additional classes there)
    4) ./bin/console hautelook_alice:doctrine:fixtures:load

    Additionally, configure the bundle in the config_dev.yml

    P.S. And locale setting doesn't work for me for some reason. I set
    hautelook_alice:
    locale: ru

    but to no effect.

  • 2016-09-27 Maksym Minenko

    I just can't get Alice to work! :(
    I'm not even sure which package to install now: nelmio/alice or hautelook/alice-bundle

    The way shown in the video doesn't work for me -- the class Nelmio\Alice\Fixtures seems like doesn't exist anymore.

    And I can't get hautelook/alice-bundle to work either... Seems like they don't support Symfony 3... For example, their documentation suggests using "php app/console fixtures:load"

    Well, I did get to work nelmio/alice 3.0.dev and (after that) hautelook/alice-bundle (via AbstractLoader::getFixtures() method).
    But after running doctrine:fixtures:load the database remained empty...

  • 2016-08-22 Chmlls

    No, it did not work, also I tried with nelmio_alice:
    Error: [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] There is no extension able to load the configuration for hautelook_alice etc

    The fix I found was passing an array as configuration in LoadFixtures.php

    code class="php">
    class LoadFixtures implements FixtureInterface
    {
    public function Load(ObjectManager $manager)
    {
    $config = array('locale' => 'es_ES');
    $objects = Fixtures::load(__DIR__.'/fixtures.yml', $manager, $config);
    }
    }

    Thanks anyway Victor

  • 2016-08-22 Victor Bocharsky

    Hey Chmlls,

    I suppose you didn't set the correct locale in Alice bundle config. Set the alice locale to "es_ES" in your config file:

    # app/config/config_dev.yml
    hautelook_alice:
    locale: es_ES

    Let me know if it helped.

    Cheers!

  • 2016-08-20 Chmlls

    Nice tutorial as always!

    How do I implement this example that I found in https://github.com/fzaninotto/...
    Search for: $faker->dni
    I tried with dni: <dni()> but I got this: [InvalidArgumentException] Unknown formatter "dni"

  • 2016-07-26 0x90

    Of course I had to encounter the weird bug. :P Thanks, that fixed it.

  • 2016-07-26 weaverryan

    I think I've seen this before actually - it's a bug in PhpStorm itself. Sometimes, for some reason, it suddenly thinks that a namespace has a syntax error in it. I copy the entire namepace line, delete it, wait a moment, then paste it back. For me, PhpStorm is happy once I do this.

    Let me know if this works!

  • 2016-07-25 0x90

    Certainly.

    - I have the 2016.1 (most recently patched) version of PhpStorm and IntelliJ IDEA on two different machines
    - Both have PHP Annotations and Symfony

    Here's the screenshot with ORM in the namespace:

    http://imgur.com/App8un7

    Here's the screenshot without ORM in the namespace:

    http://imgur.com/wcNLEqw

    $ pwd;ls
    /[...]/src/AppBundle/DataFixtures/ORM
    LoadFixtures.php fixtures_blog.yml fixtures_projects.yml

  • 2016-07-25 Victor Bocharsky

    Hey 0x90 ,

    Could you provide a bit more information? What version of PhpStorm do you use? Do you have the "PHP Annotations" plugin installed for PhpStorm? And please, show the part of your entity when you get this error (Show us how you use this namespace and some property where you try to use it and get error). We would be happy to help you with it. Thanks!

    Cheers!

  • 2016-07-23 0x90

    When I namespace with ORM, PhpStorm yells at me with the error message "Undefined constant ORM" and "Expected identifier". And it'll refuse to recognize the use statements, causing the implements FixtureInterface to be unrecognized. Once I remove the ORM from the namespace, PhpStorm is happy and I get my autocomplete. My question is why and what can I do to fix it?

  • 2016-06-29 JLChafardet

    im really loving alice for my dev environment! fixtures rule!

  • 2016-04-19 weaverryan

    Hey Hans!

    Wow, you should totally submit this as a pull request to Alice - it's a great bug fix! Either way, thanks for posting here!

  • 2016-04-18 Hans Nieuwenhuis

    Hi,

    Just in case someone else is running into this issue :

    I am running symfony and the database in two separate Docker containers.
    I created those containers myself and they are based on alpine linux.
    Since Alpine does not support GLOB_BRACE
    (The thing is that Alpine Linux uses musl-libc instead of the Linux standard GNU libc. It seems that musl does not implement GLOB_BRACE because this is not a POSIX defined option.)

    I got an error when executing the bin/console doctrine:fixtures:load

    [Symfony\Component\Debug\Exception\ContextErrorException]
    Notice: Use of undefined constant GLOB_BRACE - assumed 'GLOB_BRACE'

    As long as Alpine has this limitation, the following workaround can be used :
    Edit vendor/nelmio/alice/src/Nelmio/Alice/Fixtures.php

    and replace the line
    $matches = glob($files, GLOB_BRACE);
    with
    $matches = glob($files, (defined('GLOB_BRACE') ? GLOB_BRACE : 0));

    After this change the fixtures run smoothly