EXTRA_LAZY Relationships
…to count them... isn't the greatest thing for efficiency.
If you find yourself in this situation, you can tell Doctrine to be clever with
how it loads the relation. Go into the Category entity and find the OneToMany
relationship for $fortuneCookies. At the end…
JOINs and addSelect Reduce Queries
…fortuneCookies|length.
In PHP land, we're calling the getFortuneCookies() method on Category. But until
now, Doctrine has not yet queried for the FortuneCookie data for this Category.
However, as soon as we access the $this->fortuneCookies property, it magically
makes that query, basically saying:
Give…
Many To Many with Foundry
…not
technically required - good luck cleaning the bathroom without them! -
I like to keep them out of defaults() and set them where we're using
StarshipFactory.
Next, we'll learn how to JOIN across ManyToMany relationships. Once again,
Doctrine handles the heavy lifting for us.
Reusing Queries in the Query Builder
…
Anywhere inside here, add a new private function called
addFortuneCookieJoinAndSelect(). This will accept a QueryBuilder object (make
sure you get the one from Doctrine\ORM - the "Object Relational Mapper"), and let's
call it $qb. This will also return a QueryBuilder.
The next step is…
Testing Messenger
…you guessed
it, Zenstruck!
composer require zenstruck/messenger-test --dev
Cool! This messenger-test library adds a special Messenger transport called test.
We'll still use Doctrine by default, but now open up
config/packages/messenger.yaml. Uncomment the async transport, which uses
MESSENGER_TRANSPORT…
Factory Data Seeding
…and look at src/Factory/LockDownFactory.php. I'm not going to talk
too much about these factory classes: we already cover them in our Doctrine tutorial.
But this class will make it easy to create LockDown objects, even setting
createdAt to a random DateTime…
Extending with Events
…it says "Updated By", "Null". Our goal is to set that field automatically
when a question is updated.
A great solution for this would be to use the doctrine extensions library and
its "blameable" feature. Then, no matter where this entity is updated - inside
the…
Pagination & Column Sorting
…go fix it!
To use Pagerfanta, we'll install three libraries:
composer require babdev/pagerfanta-bundle pagerfanta/doctrine-orm-adapter pagerfanta/twig
Cool beans! Let's get the PHP side working first. Open src/Controller/MainController.php.
The current page will be stored on the…
Simpler Validator for Checking State Change
…last tutorial, we put this above that same $dragonTreasures property,
but inside the User entity. The validator would loop over each DragonTreasure,
use Doctrine's UnitOfWork to get the $originalOwnerId, and then check to see
if the $newOwnerId is different from the original. If it…
Bundle Dependencies
…because we need the services the translation component
provides, like the LocaleAwareInterface. Following that, run:
symfony composer require doctrine/doctrine-bundle doctrine/orm
Cool, these are the bare minimum dependencies our bundle requires to function.
Now, it's time to add some development dependencies. Run:
…
MakerBundle & Autoconfiguration
…Ooh. There's a ton of stuff here for setting up
security, generating doctrine entities for the database (which we'll do in the next
tutorial), making a CRUD, and much more.
Let's try one: how about we try to build our own new…
Data Fixtures
…random controller like QuestionController, we can install a bundle to
do it properly. Find your terminal and run:
composer require orm-fixtures --dev
This is another flex alias: orm-fixtures installs doctrine/doctrine-fixtures-bundle
When this finishes... it installed a recipe! I committed all…
Activating 2FA
…good. No surprises, it adds one column to our table. Run that:
symfony console doctrine:migrations:migrate
Here's the plan. A user will not have two-factor authentication enabled by default.
Instead, they'll activate it by clicking a link. When they do that…
Foundry: Fixture Model Factories
…we deserve better! Let's use a super fun new library instead. Google for
"Zenstruck Foundry" and find its GitHub Page.
Foundry is all about creating Doctrine entity objects in an easy, repeatable
way. It's perfect for fixtures as well as for functional tests…
AMQP Priority Exchange
…in different "queues". Then we can instruct our worker to first read all messages
from whatever queue async_priority_high is bound to before reading messages
from whatever queue the async transport is bound to.
This did work before with Doctrine, thanks to this queue…
Pagination
…re only using this method right here at the moment, so
rename it to createOrderedByVotesQueryBuilder()... and this will now
return a QueryBuilder - the one from Doctrine ORM. I'll remove the PHP
documentation on top... and the only thing we need to do down here…
Persisting to the Database
…question!
We're only going to focus on creating objects and saving them. Doctrine will
handle the insert queries for us.
To help do this in the simplest way possible, let's make a fake "new Vinyl Mix"
page.
In the src/Controller/ directory, create…
Request Object & POST Data
…read the direction value and increase or decrease the vote
count. We could do this with an AJAX call instead of a form submit. From
a Doctrine and Symfony perspective, it really makes no difference. So I'll
keep it simple and leave JavaScript out…
Autowiring & Service Deprecations
…In Symfony 3, when Symfony
saw a type-hint - like EntityManager - it would first look to see if there was
a service or alias in the container with that exact id: so Doctrine\ORM\EntityManager.
If there was not, it would then scan every service…
Joining Across a Relationship & The N + 1 Problem
…text. The moment we do
that, Doctrine makes a second query from the question table to get that answer's
question data: so in this case WHERE id = 463. Then we render the second answer...
and make another query to get its question data..…
x
1000+