Flex Recipe Updates
…you
know, doing our real job. I'll hit enter to go down the list one-by-one.
First up is Doctrine Bundle: and it's a complex update. It even
caused a conflict!
Sometimes we might see that a recipe update changes something - like…
Removing Items from a Collection
…the dragonTreasures
property. After cascade, add one more option here: orphanRemoval: true.
This tells Doctrine that if any of these dragonTreasures become "orphaned" - meaning
they no longer have any owner - they should be deleted.
Let's try it. When we hit "Execute" again... got it…
Adding Items to a Collection Property
…to be this object. That's important
because of how Doctrine handles relationships: setting the owner sets what's called
the "owning" side of the relationship. Basically, without this, Doctrine wouldn't
save this change to the database.
The takeaway is that, thanks to addDragonTreasure…
Transport: Do Work Later (Async)
…supermarket. Out-of-the box, Messenger supports three: amqp -
which basically means RabbitMQ, but technically means any system that implements
the "AMQP" spec - doctrine and redis. AMQP is the most powerful... but unless
you're already a queueing pro and want to do something crazy…
Bonus: Messenger Monitor Bundle
…but I
don't have migrations configured for this project. So in your terminal, run:
symfony console doctrine:schema:update --force
Before we check out the UI, the bundle has two optional dependencies that
I want to install. First:
composer require knplabs/knp-time-bundle…
Simpler State Processor
…to it.
The other way - which we did in the last tutorial for this class - is to
decorate the core processor. Here, we decorated the PersistProcessor
from Doctrine... which means that whenever any API resource is saved, when it
tries to use the core PersistProcessor…
A World without Build Systems?
…and... hello Mixed Vinyl! But wow is this thing weird and ugly-looking.
This is a Symfony 6.3 project - the same project we've built in the Symfony series.
It has Doctrine installed... but there's nothing particularly special about it, and
right now…
Problems with Entities in Messages
…it's not already in the list,
it's added. Then, when we call flush(), Doctrine loops over all of these
objects, looks for any that changed, and creates the appropriate UPDATE or INSERT
queries. It knows whether or not an object should be inserted…
Setting the Relation
…not related yet.
Pff, try to load the fixtures anyway. Head over to your terminal and run:
symfony console doctrine:fixtures:load
Rutro:
starship_id cannot be null on the starship_part table.
Why is that column required? In StarshipPart, the starship property has a…
Running Code "On Publish"
…DragonTreasure objects, loop over them, then use Doctrine's UnitOfWork to see
what each DragonTreasure looked like when it was originally loaded
from the database.
Should we use that same trick here to see what the isPublished property
originally looked like? We could... but there…
Passing Entity Ids inside of Messages
…Enter -> Initialize Fields
to create that property and set it.
Back in the method, we can say
$imagePost = $this->imagePostRepository->find($imagePostId).
That's it! And this fixes our Doctrine problem! Now that we're querying for the
entity, when we call flush()…
Upgrading to Symfony 7
…Doctrine uses behind the scenes to load lazy relationships. Recently,
Symfony added its own version of proxies called "ghost objects". They're spooky
cool. Anyway, this proxy package isn't needed anymore. It was originally added
to our app way back when we installed Doctrine…
Pagination on a Custom Resource
…Second, to return the count of the
total number of items. When you use Doctrine, it executes 2 separate queries
for this: one to fetch the current page's results with a LIMIT and OFFSET, and a
second COUNT query to count every row.
Ok…
Translation Logic
…and run:
symfony console debug:autowiring ManagerRegistry
Here we go: alias:doctrine - doctrine is the service ID - that's a short one!
Copy that and back in services.php, define it as the fourth argument:
service(), paste:
Perfect!
Doctrine's now available in our ObjectTranslator…
Joining to Avoid the N+1 Trap
…in the template looping over
the parts, when we reference part.starship, Doctrine has a light bulb moment.
It realizes it has the part data, but not the Starship data for this part.
So it queries for it. We end up with one query for…
andWhere() and orWhere()
…andWhere()
is always ok - even if this is the first WHERE clause... and we don't really
need the "and" part. Doctrine is smart enough to figure that out.
What's wrong with ->where()? Well, if you added a WHERE clause to
your QueryBuilder earlier…
Query Extension: Auto-Filter a Collection
…we did with the PersistProcessor.
Except... that won't quite work. Why? The CollectionProvider from Doctrine executes
the query and returns the results. So all we would be able to do is take those
results... then hide the ones we don't want. That's..…
Validating how Values Change
…what they originally looked like.
So, we are going to solve this with validation... but with the help of a special
class from Doctrine called the UnitOfWork.
Alrighty, let's whip up a test to shine a light on this pesky bug. Inside
tests/Functional/…
make:user
…like
HumanoidEntity. If the "thing" that logs into your site would be better called
a Company or University or Machine, use that name here.
Do you want to store user data in the database via Doctrine?
For us: that's a definite yes... but it…
The Clever Criteria System
…10 of them
cost more than 50,000. What a waste! Could we ask Doctrine to change the query
so it only grabs the parts related to the starship where the price is greater
than 50,000?
Enter the Criteria object. This thing is mighty…
x
1000+