Filtering & Searching
…by title? These are called "filters": ways to
see a "subset" of a collection based on some criteria. And API Platform comes
with a bunch of them built-in!
Let's start by making it possible to only return published cheese listings.
Well, in a…
Input DTO Class
…needed anymore
because we have moved all of this stuff. Delete both:
There is now nothing inside of CheeseListing about serializing or deserializing.
Ok! Our CheeseListingInput is ready! To tell API Platform to use it, it's
the same as output. On CheeseListing, add input=…
Validation Groups
…plainPassword field isn't persisted to the database,
at the beginning of each request, after API Platform queries the database for the
User, plainPassword will always be null. If an API client only sends the
username field... because that's all they want to update..…
Serialization Tricks
…refresh, and check out the POST endpoint. The field is gone.
API Platform sees that we have a treasureName argument that could be sent,
but since treasureName doesn't correspond to any property, that field doesn't
have any serialization groups. So it's not…
React Admin
…all the details, even including CORS config if
you have that problem.
So... let's do this!
If you use the API Platform Docker distribution, this admin area comes pre-installed.
But it's also easy enough to add manually. Right now, our app doesn…
Relating Resources
…API and instantly see what treasures it has.
Anyways, the property - dragonTreasures inside of User is fine.... and finally,
for orphanRemoval, say no. We'll also talk about that later.
And... done! Hit enter to exit.
So this had nothing to do with API Platform…
Pagination on a Custom Resource
…number
per page is still 30 and the offset is 30.
Where is it getting 30 as the items per page? That's the default in API
Platform for any resource. But this is something you can configure on your
#[ApiResource] attribute: change paginationItemsPerPage to…
Custom Resource State Processor
…I love it!
Speaking of that Patch operation, when it's used, API Platform will call the
state processor, so we can save... or do whatever we want. We don't have one yet,
so that'll be our next job.
But let's start…
Query Extension: Auto-Filter a Collection
…to modify the query for a collection endpoint, we're going to create something
called a query extension. Anywhere in src/ - I'll do it in the ApiPlatform/
directory - create a new class called DragonTreasureIsPublishedExtension. Make
this implement QueryCollectionExtensionInterface, then go to "Code"->"Generate…
Filter Autowiring
…option and
sets those as named arguments on that service. Why do we care about how our
filter objects are instantiated? Because when API Platform registers the service,
it also sets it to autowire: true. Yep, this means we can access services
in our filter…
Core Listeners & Accessing the "Resource" Objects
…it does have two downsides, which
may or may not be important. First, the event system isn't used in API Platform's
GraphQL support... so this won't work if you're using GraphQL. And second, if
you're writing some custom console commands…
Type Validation
…argument is not nullable, if you tried to send a price
field set to null that value would not be legal to set on this property.
When this happens, API Platform returns a 400 error. It's not a validation error
exactly... but it effectively…
Input DTO Update Problems
…when the serializer deserializes the JSON into the
CheeseListingInput, the owner property will be null... and then we pass that
null value onto $cheeseListing->setOwner(). When API Platform tries to save
the CheeseListing with no owner... error!
But... let's back up: there's a…
Output Properties & Metadata
…This is referring to the
"models" at the bottom of the page. Scroll down to see them.
API Platform creates a unique "model" for each different way that a
resource might be returned based on your serialization groups. And when you
create an output DTO…
Custom Resource GET Item
…we kind of added the get
item operation... but we made it return a 404 response:
We did that as a workaround so that API Platform could generate an IRI for
DailyStats. Now I want to make this truly work.
Remove all of the custom…
Custom Resource Data Provider
…is that, inside DailyStats, we've specifically
said that we don't want any item operations:
This kind of confuses API Platform, which has a brief existential crises
before saying:
How can I generate a URL to the item operation if there aren't any!…
Leveraging the Doctrine Data Provider
…down at getCollection(): it creates a query builder and then loops
over something called the "collection extensions". This is the Doctrine extension
system in API Platform, and these extensions are actually what is responsible
for changing the query to add things like pagination and filtering…
Creating Embedded Objects
…create and save these objects, I'd probably just find where that CheeseListing
is being created and call $entityManager->persist() on it. But because API
Platform is handling all of that for us, we can use a different solution.
Open User, find the $cheeseListings property…
@SerializedName & Constructor Args
…anyone using our API.
It's pretty easy to guess how these properties are created: the keys inside the
JSON literally match the names of the properties inside our class. And in the case
of a fake property like textDescription, API Platform strips off the…
Running Code "On Publish"
…processors. The make:state-processor command
created the process() method with a void return. And... that makes sense. API
Platform passes us the data and our job is just to save that... not return anything.
However, technically the process() method can return something. And, for…
x
1000+