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…
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?…
Joining Across a Many-to-Many Relationship
…add a leftJoin(). But we're not going to think about
the join table or the database. Nope, focus only on the relationships in Doctrine.
So we're joining across s, which is our starship, and droids, the property
that has the ManyToMany relationship to…
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…
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…
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…
Relating Resources
…ll do our standard double-check to make sure the migration isn't trying to
mine bitcoin. Yep, all boring SQL queries here.
Run it with:
symfony console doctrine:migrations:migrate
And it explodes in our face. Rude! But... it shouldn't be too surprising…
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…
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…
Pagination & Foundry Fixtures
…method. All we need
is: DragonTreasureFactory::createMany(40) to create a healthy trove of 40
treasures:
Let's try this thing! Back at your terminal, run:
symfony console doctrine:fixtures:load
Say "yes" and... it looks like it worked! Back on our API docs, refresh…
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…
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…
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…
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…
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…
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…
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…
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()…
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…
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…
x
1000+