Using RAND() or Other Non-Supported Functions
…t change anything in our app... it just adds a bunch of code
that we can activate for any functions that we want. To do that, back over in
config/packages/doctrine.yaml, somewhere under orm, say dql. There are a bunch
of different categories…
WHERE IN()
…but inside of that, instead of a comma-separated list, you'll set an
array. Doctrine will transform that for us.
And now... nice! Once you know how it works, it's just that easy.
Next: You're probably familiar with the RAND() function for…
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…
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…
Embedded Write
…our data, created a new User object and
then set the username onto it. Doctrine failed because we never told it to persist
the new User object.
Though... that's not the point: the point is that we don't want a new User!
We…
Creating a User Entity
…run:
symfony console make:migration
Then... spin over and open that new migration file. No surprises: it creates the
user table:
Close that up and run it with:
symfony console doctrine:migrations:migrate
Sweet! Though, I think our new entity deserves some juicy data fixtures…
Filters: Searching Results
…since we're using the Doctrine ORM -
because we want to allow the user to filter on a boolean field.
The second thing you need top pass is properties set to an array of which fields
or properties you want to use this filter on…
Foundry: Fixtures You'll Love
…But first... let's run blindly forward and use this class! In AppFixtures,
delete everything and replace it with VinylMixFactory::createOne().
That's it! Spin over and reload the fixtures with:
symfony console doctrine:fixtures:load
And... it fails! Boo
Expected argument type "DateTime", "null"…
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…
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…
Builder in Symfony & with a Factory
…character builder needed a service like the EntityManager?
With our new setup, we can make that happen. I don't actually have Doctrine installed
in this project, so instead of the EntityManager, let's require
LoggerInterface $logger... and I'll again add private in front…
Events, Events & Events!
…see the Twig templates being executed below it, and even little Doctrine
queries happening along the way.
The biggest thing I want you to notice is that most of the other lines - both
before and after the controller - contain the word Listener, or sometimes
Subscriber…
Installing Messenger
…validates it, uses another service
to store that file - that's the uploadImage() method, creates a new ImagePost
entity, saves it to the database with Doctrine and then, down here, we have
some code to add Ponka to our photo. That ponkafy() method does the…
Testing with Multiple Symfony Versions
…In our composer.json file, for the Symfony
packages, this means 6.4.0. For the doctrine-bundle, it would be 2.18.0.
To get these lowest versions installed, over in the terminal, run:
symfony composer update --prefer-lowest
We can see Composer switched…
Implementing the Webhook Consumer
…method, fetch the user with
$user = $this->entityManager->getRepository(User::class)->find($userId).
Next, if $user doesn't exist, we'll throw new EntityNotFoundException()
(choose the one from Doctrine\ORM). For the message write
sprintf('User "%s" not found for LemonSqueezy webhook "%s"!', $userId…
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…
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…
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..…
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+