Linking Symfony deps to your Local Copy
…to use annotations with the
validator, you need to install the annotations library. Stop the web server and
run:
composer require annotations
This installs sensio/framework-extra-bundle... we only technically need
to install doctrine/annotations. But, that's ok. Restart the built-in web…
Flex Extras
…more going on. Find your composer.json file and make sure
the version constraint is ^3.0.
Then, run:
composer update doctrine/doctrine-fixtures-bundle
Version 3 of this bundle is all new... but not in a "broke everything" kind of
way. Before, fixture classes…
Bye Bye AppBundle
…But, the fix is easy: Find all
AppBundle and replace with App.
Done! There is one last thing we need to undo: in config/packages/doctrine.yaml.
Remove the AppBundle mapping we added.
So, what other AppBundle things haven't been updated yet? It's…
Final config/ Migration
…We can also delete config_prod.yml.
Oh, by the way, if you have some doctrine caching config in config_prod.yml,
I would recommend not migrating it. The DoctrineBundle recipe gives you prod
configuration that is great for production out-of-the-box. Booya…
Autowiring Deprecations
…we can create an alias.
Copy the Doctrine\ORM\EntityManager class name. Then, find your editor and open
up services.yml. Add the alias: Doctrine\ORM\EntityManager aliased to @,
and then copy the target service id: @doctrine.orm.default_entity_manager:
We have full control…
Autowiring Controller Arguments
…container directly! We know the fix: type-hint a new argument with
LoginFormAuthenticator $authenticator and use that below:
Almost done! The app.doctrine.hash_password_listener service isn't being used anywhere,
and neither is app.form.help_form_extension.
And... that's it! At…
_defaults, autowire & autoconfigure
…it for you.
This works for many - but not all tags. It does not work for doctrine.event_subscriber
or form.type_extension:
Because it has an extended_type tag option... which the system can't guess for you.
When you're developing a feature…
Adding to a Collection: Cascade Persist
…operations for GenusScientist.
Umm, what? Here's what's going on: when we persist the Genus, Doctrine sees the
new GenusScientist on the genusScientists array... and notices that we have
not called persist on it. This error basically says:
Yo! You told me that you…
Collection Delete & allow_delete
…submit, the final array will have
three GenusScientist objects in it, instead of four.
Ready? Submit!
Hmm, no error... but it still doesn't work. Why not? Hint: we already know the
answer... and it relates to Doctrine's inverse relationships. Let's fix it.
Saving the Inverse Side of a ManyToMany
… What's going on!?
This is the moment where someone who doesn't know what we're about to learn,
starts to hate Doctrine relations.
Earlier, we talked about how every relationship has two sides. You can start with
a Genus and talk about the…
EntityType Checkboxes with ManyToMany
…to change the genusScientists property, that's what we should call the field.
The type will be EntityType:
This is your go-to field type whenever you're working on a field that is mapped as any
of the Doctrine relations. We used it earlier…
Hooking up the Scientist Removal JavaScript
…Mixed in with AJAX call for notes is
our DELETE call. Click the little sha, then go to the Doctrine tab. Ooh, look at
this:
DELETE FROM genus_scientist WHERE genus_id = 11 AND user_id = 11
Gosh darn it that's nice…
ManyToMany Relationship
…thing is that
it will hold the array of User objects that are linked to this Genus:
Above, add the annotation: @ORM\ManyToMany with targetEntity="User".
Finally, whenever you have a Doctrine relationship where your property is an array
of items, so, ManyToMany and OneToMany…
JoinColumn & Relations in Fixtures
…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 doctrine:query:sql:
Every single one has a random genus. Do you love it…
Creating Embedded Objects
…>persist() on that new CheeseListing, which
is why Doctrine isn't sure what to do when trying to save the User.
If this were a traditional Symfony app where I'm personally writing the code to
create and save these objects, I'd probably just…
User API Resource
…auto-increment id, one option is to use a UUID.
We're not going to use them in this tutorial, but using a UUID as your identifier
is something that's supported by Doctrine and API Platform. UUIDs work with
any database, but they are…
Relating Resources
…that up... just to make sure it doesn't contain anything extra.
Looks good - altering the table and setting up the foreign key. Execute that:
php bin/console doctrine:migrations:migrate
Oh no! It exploded!
Cannot add or update a child row, a foreign key…
Embedded Write
… But the error is fascinating!
A new entity was found through the relationship CheeseListing.owner that was
not configured to cascade persist operations for entity User.
If you've been around Doctrine for awhile, you might recognize this strange error.
Ignoring API Platform for a…
Filtering & Searching
…at the top of your class, add use BooleanFilter. But... careful...
most filters support Doctrine ORM and Doctrine with MongoDB. Make sure to choose
the class for the ORM.
Ok, now move over and refresh again.
We're back to life! Click "Try it out…
Validation
…earlier, if you're using
Symfony 4.3, you might already see a validation error instead of a database error
because of a new feature where validation rules are automatically added by reading
the Doctrine database rules.
But, whether you're seeing a 500 error…
x
1000+