Symfony 4 and Alice 3 tutorial Installing and configuring Symfony 4 and the Nelmio/Alice 3 fixtures generator

Scroll this

Somehow I always have problems installing and configuring Alice bundle. Symfony and Alice are beeing developed and new versions are pushed regularly – there is lots of outdated and confusing information. Even official documentation is not always correct and up to date. So since I was unable to make the two work out of the box, and since I didn’t find all the info in the one place – I decided to write this tutorial.

In this tutorial you will learn how to install, configure and create fixtures, write custom fixture Buffy quote generator (formatter). 😀

Please note, this is not detailed tutorial about Alice, we will just make it work. Also, don’t forget, PHP 7.1+ is required by Symfony 4.
Code from this article was tested on Fedora 27, Symfony 4.0.7, Alice bundle 1.4, nelmio/alice 3.2.2 and PHP 7.1.15.

Code

Complete code is available from this article or from github: github.com/dribtech/symfony4-alice3-tutorial .

Installing

Follow this steps:

Fix #1

Please note: recently I noticed that this bug is now fixed. But the rest of tutorial still apply.

And finally install Alice bundle:

$ composer require --dev hautelook/alice-bundle

And here it is, the first problem: Cannot register “Hautelook\AliceBundle\HautelookAliceBundle” without “Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle”.

To fix it, add this lines to the config/bundle.php

Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => [‘dev’ => true, ‘test’ => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => [‘dev’ => true, ‘test’ => true],

Now config/bundles.php should look like this:

Credit: github.com/hautelook/AliceBundle/issues/380.

Fix #2

Please note: recently I noticed that this bug is now fixed. But the rest of tutorial still apply.

To see did we fix the problem  run:

$ bin/console c:c

Another error: Unrecognized option “db_drivers” under “hautelook_alice”

Change config/packages/dev/hautelook_alice.yaml to:

For now, just ignore fixtures_path since it is not used in this tutorial.

Run

$ bin/console c:c

Finally no errors! 😀

Lets load some fixtures!

Configure database

Edit .env to set DATABASE_URL

Just fill in all the required data according to the DATABASE_URL template from the existing .env file.

Then run

$ bin/console doctrine:database:create

You should get message like this: “Created database db_name for connection named default”

Create Entity

In src/Entity create Buffy.php:

Run

$ bin/console doctrine:schema:update --force

To create buffy table. Output:

Updating database schema…
1 query was executed
[OK] Database schema updated successfully!

Create and load fixture #1

In src/DataFixtures/ORM create fixtures.yaml :

In src/DataFixtures/ORM  create LoadFixtures.php

Run

$ bin/console doctrine:fixtures:load

Output:

Careful, database will be purged. Do you want to continue y/N ?y
> purging database
> loading App\DataFixtures\ORM\LoadFixtures

Lets check it:

$ bin/console doctrine:query:dql "select b from App\Entity\Buffy b"

And here we go, 20 random names and quote lines.

Create custom generator (formatter)

Lets create custom formatter which will output some real people quotes!

In src/DataFixtures/ORM create BuffyProvider.php

Register the Buffy provider

According to the instructions, no extra action is necessary. But it didn’t work for me. So here is the another way (also described in the documentation):

In src/DataFixtures/ORM create AppNativeLoader.php

Change src/DataFixtures/ORM/LoadFixtures.php to use AppNativeLoader instead of NativeLoader. Now, LoadFixtures looks like this:

And the last step, edit src/DataFixtures/ORM/fixtures.yaml to use our buffyQuote() instead of text():

Run

$ bin/console doctrine:fixtures:load

And to check it out:

$ bin/console doctrine:query:dql "select b from App\Entity\Buffy b"

Links

7 Comments

  1. Thanks for this, fixes #1 and #2 we’re wahat I needed 🙂

    • Hi Jérémy, I was aware of this method – but I couldn’t make it work for me! 🙁

Leave a Reply to Webber Cancel reply