Ordering a Relation and "fetch" type
…what's happening: we grab the starships, then
as soon as we count ship.parts, Doctrine realizes it doesn't have
that data yet. So it fetches all the parts for each ship one by one and
counts them. This is a common situation: we…
Fetching a Relation's Data
… Instead, pass the Starship object itself. You could pass the id if
you're feeling lazy, but in the spirit of Doctrine, relationships, and
thinking about objects, passing the entire Starship object is the way to go.
Let's debug and see what we've…
Email from CLI Command
…count($bookings))).
Testing time! Pop over to your terminal. To be sure we have a booking that
needs a reminder sent, reload the fixtures with:
symfony console doctrine:fixture:load
Now, run our new command!
symfony console app:send-booking-reminders
Nice, 1 reminder sent…
Email Twig Layout
…we aren't using migrations. So, we'll just force update the schema.
In your terminal, run:
symfony console doctrine:schema:update --force
Then, reload the fixtures:
symfony console doctrine:fixture:load
That all worked, great!
Next, we'll create a new reminder email and…
Async & Retryable Sending with Messenger
…sent to be queued. We'll use the
Doctrine transport as it's easiest to set up.
composer require symfony/doctrine-messenger
Back in our IDE, the recipe added this MESSENGER_TRANSPORT_DSN to our .env
and it defaulted to Doctrine - perfect! This transport adds…
Goodbye SensioFrameworkExtraBundle
…the route does have a {slug}
wildcard... but then a $mix argument, which is a Doctrine entity.
Behind the scenes, the param converter would automatically query for a
VinylMix where slug equals the {slug} in the URL. No annotation needed:
it just worked.
The good…
Leveraging the Core Processor
…on? We map the UserApi to a new User object and save the new
User... which causes Doctrine to assign the new id to that entity object. But
we never take that new id and put it back onto our UserApi.
To fix this, after…
Entity -> DTO Item State Provider
…the querying work
manually. Instead, we'll... "delegate" it the core Doctrine item provider. Add
a second argument... we can just copy the first... type-hinted with ItemProvider
(the one from Doctrine ORM), and called $itemProvider.
I like it! Back below, let it do the…
SELECT the SUM (or COUNT)
…object.
This is interesting too! In SQL, we would normally say something like
WHERE fortuneCookie.categoryId = and then the integer ID. But in Doctrine, we don't
think about the tables or columns: we focus on the entities. And, there is no
categoryId property…
Filtering Relation Collection
…internally, Doctrine will first query for all
100... even though we'll only return 10. If you have large collections,
this can be a performance problem. In our Doctrine tutorial, we talk about fixing
this with something called the Criteria system.
But with both approaches…
Totally Custom Fields
…is loaded. For
example, our classes are already using a Doctrine state provider behind the
scenes to query the database. We'll cover state providers in part 3 of this series.
The second solution would be to use the custom normalizer like we did, then…
Resetting the Database
…method and run code here that does that. Fortunately,
we don't need to because there are multiple libraries that already solve this
problem. My favorite is Foundry.
Run:
If you watched our Doctrine tutorial, you'll remember Foundry! But you may not
know about…
AssociationField for a "Many" Collection
…Question:
That method properly removes the Answer from Question. But more importantly,
it sets $answer->setQuestion() to null, which is the owning side of this
relationship... for you Doctrine geeks out there.
Ok, try removing "95" again and saving. Hey! We upgraded to an error…
Hashing Plain Passwords & PasswordCredentials
…try this. Find your terminal and run:
symfony console doctrine:fixtures:load
This will take a bit longer than before because hashing passwords is actually CPU
intensive. But... it works! Check the user table:
symfony console doctrine:query:sql 'SELECT * FROM user'
And... got it…
Giving Users Passwords
…sure everything looks good:
And... it does! Close it... and run it:
symfony console doctrine:migrations:migrate
Perfect! Now that our users have a new password column in the database, let's
populate that in our fixtures. Open up src/Factory/UserFactory.php and
find…
Setting Many To Many Relations
…the flush, remove an assignment:
$starship->removeDroid($droid1):
Reload the fixtures and check out the join table.
Only two rows remain! Doctrine removed the row for our removed droid.
One final touch on ManyToMany — remember when we discussed owning
versus inverse sides of a relationship?…
Many-To-Many Relationship
…database: with a join table.
The real magic of Doctrine is that we only need to think about objects. A Starship
object has many Droid objects, and a Droid object has many Starship objects.
Doctrine handles the tedious details of saving that relationship to the…
Droid Entity for the ManyToMany Relationship
…to generate the migration and run it:
Go take a peek. No surprises here. So... run it:
symfony console doctrine:migrations:migrate
And... we've got a shiny new droid table in the database. It's not yet
in a relationship with ship, but hey…
Criteria: Filter Relation Collections
…There it
is. Below, add a new method called getFortuneCookiesStillInProduction(). This,
like the normal method, will return a Doctrine Collection. And... just to help
my editor, copy the @return doc above to say that this is a Collection of
FortuneCookie objects.
So... what do we…
Auto Setting the "owner"
…for this: the state processor system.
As a reminder, our POST and PATCH endpoints for DragonTreasure already have a state
processor that comes from Doctrine: it's responsible for saving the object to
the database. Our goal will feel familiar at this point: to decorate…
x
1000+