Some words about Fixtures.
We would like to write now a Controller and a View to see the movies and actors we have in the database. But we have a problem, we don't have movies or actors in our database.
It is very convenient to be able to generate some dummy data to populate the database at any time in our development (and testing) environments. We call these dummy data fixtures.
Let's create some fixtures. Run this command:
composer require --dev hautelook/alice-bundle
This recipe is in the contrib
repository that is maintained by the community, so you must agree on use community maintained packages to install it.
Alice allows us to write our fixtures in a much more convenient way, and have access to a lot of generators of fake data.
Let's try writing some fixtures. Open fixtures/movies.yml
with your editor and write this:
1 2 3 4 5 6
App\Entity\Movie: movie{1..10}: name: '<sentence(4, true)>' director: '<name()>' year: '<numberBetween(1970, 2017)>' picture: '<image("./public/images", 500, 500, "cats", 0)>'
Create the directory public/images
. and run:
php bin/console hautelook:fixtures:load
Examine the table movies
, and the contents of public/images
.
Can you write fixtures for the Entity Actor?
You can do it in the same file fixtures/movies.yml
It should match the properties of our entity (name, picture). At this step, forget about the relationship with movies.
Can you make images be about people instead of cats? Try a different category in http://lorempixel.com/
What about the relationship between movies and actors?
Just add a line to the movie fixtures:
1 2 3 4 5 6 7
App\Entity\Movie: movie{1..10}: name: '<sentence(4, true)>' director: '<name()>' year: '<numberBetween(1970, 2017)>' picture: '<image("./public/images", 500, 500, "cats", 0)>' actors: ["@actor<numberBetween(1, 3)>", "@actor<numberBetween(4, 6)>", "@actor<numberBetween(7, 10)>"]
And reload the fixtures:
php bin/console hautelook:fixtures:load
In our database the tables actor
and movie_actor
are now populated.