JOINing Across Multiple Relationships
…question_tag data that's between
Question and Tag. Doing that is legal in SQL... but it messes up how
Doctrine creates the objects, so it doesn't allow it.
The solution is easy enough: select both. You can actually pass an array to
addSelect…
Doing Crazy things with Foundry & Fixtures
…database. Instead, the new QuestionTag object will be related to whatever Question
is currently being created. Foundry does this by reading the Doctrine relationship
and smartly overriding the question attribute on QuestionTagFactory.
But... I did say that there was a problem with this. And... we…
When a Migration Falls Apart
…ve fixed the migration, how can we run it again? Let's
try the obvious:
symfony console doctrine:migrations:migrate
It fails again! But for a different reason: dropping the foreign key failed
because that's already gone.
Here's the problem. When we first…
Handling ManyToMany in Foundry
…maybe you see it... but let's try it anyways.
Spin over and reload the fixtures:
symfony console doctrine:fixtures:load
Awesome. And now check the database. I'll first say:
symfony console doctrine:query:sql 'SELECT * FROM tag'
Yep! We do have 100 tags…
Filtering to Return only Approved Answers
…doesn't contain any
surprises
It looks good:
ALTER TABLE answer ADD status.
Close that, spin back to your terminal and execute it:
symfony console doctrine:migrations:migrate
Because we have exactly three possible statuses, I'm going to add a constant for
each one…
Relation OrderBy & fetch=EXTRA_LAZY
…six answers, eight answers.
But check out the web debug toolbar. Woh! We suddenly have a lot of queries.
Click to open Doctrine's profiler. The first query is still for all of the question
objects. But then, one-by-one it selects FROM answer…
Fetching Relations
…inside the
Question entity, the $answers property will not be a true array of Answer
objects. Nope, it will be some sort of Doctrine collection object. It may be an
ArrayCollection object or this PersistentCollection object... just depending
on the situation. It doesn't really…
Relations in Foundry
…Notice that, in getDefaults(),
we are not setting the question property. And so, if you spin over to
your terminal and run:
symfony console doctrine:fixtures:load
... we get our favorite error: question_id column cannot be null.
To fix this, in AppFixtures, pass a…
Saving Relations
…passing the ID to the
question property, we're setting the entire Question object onto the
property. Doctrine will be smart enough to save these in the correct order: it'll
save the question first, grab its new id, and use that to save the…
UUID as a API Identifier
…talked about it before.
Go to the top of User. Every API Resource needs an identifier. And when you
use Doctrine, API Platform assumes that you want the database id as the identifier.
To tell it to use something different, add @ApiProperty() with identifier=false:
That…
UUID's
…how do we generate these UUID strings? Symfony 5.2 will come with a
new UUID component, which should allow us to easily generate UUID's and store them
in Doctrine. But since that hasn't been released yet, we can use ramsey/uuid,
which…
Type Validation
…its work, it first tries to figure
out what type the field should be, which we know it does in a number of different
ways, like reading Doctrine metadata, setter argument type-hints and even PHPDoc.
Then, for scalar types like the int price, if…
Input Data Transformer
…object. Second, we transform that
CheeseListingInput into a CheeseListing in the data transformer. And
third, the normal Doctrine data persister saves things. That's a really clean
process!
Go back to the docs and look at the put operation that updates cheeses. Will
this work…
Custom Filter Logic for Entities
… Our filter
is working!
So this is what it looks like to make a custom filter when your resource is a
Doctrine entity. But the process is different if you need to make a custom filter
for an API resource that is not an entity…
Output Properties & Metadata
…that's no surprise! When we serialize a CheeseListing entity, API Platform
can use the Doctrine metadata above the title property to figure out that it's a
string:
But in this case, when it looks at title, it doesn't get any info about…
Custom Filter for Custom Resources
…had before:
The only difference is that the apply() method has different arguments
than filterProperty()... which makes sense because that method was all
about querying via Doctrine.
Before we start filling this in, go to src/Entity/DailyStats.php so we can use
the filter…
Custom Filter, getDescription() & properties
…implement an interface or
extend some base class. In this case, we need to extend AbstractFilter. Make
sure you get the one from Doctrine ORM... it's actually impossible to see which
one we have here... so I'll randomly guess:
Hey! I got it…
Collection "Types" and readableLink
…lot of metadata about
each property, like its type and whether it's required. And it gets that from
many different sources like Doctrine metadata and our own PHPDoc.
And because collecting all of this can take time, it caches it. Now,
API Platform is…
Property Metadata
… Integers?
Aliens?
API Platform gets metadata about each property from many different places, like
by reading Doctrine metadata, PHPDoc, looking at the return types of getter methods,
looking at the argument type-hint on setters, PHP 7.4 property types and more.
What's really…
Custom Resource Data Provider
…as the identifier? In Doctrine,
it happens automatically thanks to the @ORM\Id annotation:
When you're not in an entity - or if you want to use a field other than
the database id in an entity - you can add @ApiProperty() with identifier=true:
Cool! Let…
x
1000+