Scroll down to the script below, click on any sentence (including terminal blocks!) to jump to that spot in the video!Cool, got it! Show me the script!
Is the relationship required in the database? I mean, could I save a
without setting a
Genus on it? Actually, I could! Unlike a normal column, relationship
columns - for whatever reason - are optional by default. But does it make sense
to allow a
GenusNote without a
Genus? No! That's crazy talk! Let's prevent it.
ManyToOne annotation and add a new annotation below it:
|... lines 1 - 10|
|... lines 13 - 39|
|... lines 45 - 99|
JoinColumn annotation controls how the foreign key looks in the database.
And obviously, it's optional. Another option is
onDelete: that literally
ON DELETE behavior in your database - the default is
but you can also use
Anyways, we just made a schema change - so time to generate a migration!
This time, I'll be lazy and trust that it's correct. Run it!
Ah, it explodes! Null value not allowed? Why? Think about what's happening: we have
a bunch of existing
GenusNote rows in the database, and each still has a
genus_id. We can't set that column to
NOT NULL because of the data that's already
in the database.
If the app were already deployed to production, we would need to fix the migration:
maybe UPDATE each existing
genus_note and set the
genus_id to the first
in the table.
But, alas! We haven't deployed to production yet: so there isn't any existing production database that we'll need to migrate. Instead, just start from scratch: drop the database completely, re-create it, and re-migrate from the beginning:
./bin/console doctrine:database:drop --force
Phew! Now it works great.
If you still get an error while running the migration, it's because of a MySQL change! Find the details here: http://bit.ly/migrations-tweak
Last step! Our fixtures are broken: we need to associate each
GenusNote with a
Genus. We know how to set normal properties, like
But how can we set relations? As usual with Alice: it's so nice. Use
then the internal name of one of the 10 genuses - like
genus_1. That's it!
But, you know what? That's not awesome enough. I really want this to be a random
Genus. Ok: change that
|... lines 1 - 8|
|... lines 11 - 14|
Alice will now look at the 10 Genus objects matching this pattern and select a random one each time.
Reload the fixtures:
It's alive! Check out the results again with
./bin/console doctrine:query:sql 'SELECT * FROM genus_note'
Every single one has a random genus. Do you love it? I love it.