Chapters
36 Chapters
|
3:44:20
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!
Subscribe to download the code!
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "3.1.x-dev", // 3.1.x-dev
"doctrine/annotations": "^2.0", // 2.0.1
"doctrine/doctrine-bundle": "^2.8", // 2.10.2
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.4
"doctrine/orm": "^2.14", // 2.16.1
"nelmio/cors-bundle": "^2.2", // 2.3.1
"nesbot/carbon": "^2.64", // 2.69.0
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.23.1
"symfony/asset": "6.3.*", // v6.3.0
"symfony/console": "6.3.*", // v6.3.2
"symfony/dotenv": "6.3.*", // v6.3.0
"symfony/expression-language": "6.3.*", // v6.3.0
"symfony/flex": "^2", // v2.3.3
"symfony/framework-bundle": "6.3.*", // v6.3.2
"symfony/property-access": "6.3.*", // v6.3.2
"symfony/property-info": "6.3.*", // v6.3.0
"symfony/runtime": "6.3.*", // v6.3.2
"symfony/security-bundle": "6.3.*", // v6.3.3
"symfony/serializer": "6.3.*", // v6.3.3
"symfony/stimulus-bundle": "^2.9", // v2.10.0
"symfony/string": "6.3.*", // v6.3.2
"symfony/twig-bundle": "6.3.*", // v6.3.0
"symfony/ux-react": "^2.6", // v2.10.0
"symfony/ux-vue": "^2.7", // v2.10.0
"symfony/validator": "6.3.*", // v6.3.2
"symfony/webpack-encore-bundle": "^2.0", // v2.0.1
"symfony/yaml": "6.3.*", // v6.3.3
"symfonycasts/micro-mapper": "^0.1.0" // v0.1.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.4
"mtdowling/jmespath.php": "^2.6", // 2.6.1
"phpunit/phpunit": "^9.5", // 9.6.11
"symfony/browser-kit": "6.3.*", // v6.3.2
"symfony/css-selector": "6.3.*", // v6.3.2
"symfony/debug-bundle": "6.3.*", // v6.3.2
"symfony/maker-bundle": "^1.48", // v1.50.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/phpunit-bridge": "^6.2", // v6.3.2
"symfony/stopwatch": "6.3.*", // v6.3.0
"symfony/web-profiler-bundle": "6.3.*", // v6.3.2
"zenstruck/browser": "^1.2", // v1.4.0
"zenstruck/foundry": "^1.26" // v1.35.0
}
}
8 Comments
I have an Endpoint with takes a UUID property and an array of UUIDs via POST.
In API Platform 2.6, I had a class tagged as ApiResource with has a path and an Output-Class defined (another ApiResource, which is also a Doctrine-Entity).
Then I had a DataTransformer, which took the ApiResource, which acted as Input-DTO, used the IDs to make a Database-Query and returned a collection of Entities.
Now, I tried to do the same with the help of a data-processor, because there are no data-transformers in API-Platform > 3.
But always, when I try to return more than one entity, I get a 500 response coming from "get_class(): Argument #1 ($object) must be of type object, array given".
How can I take a post with ID's, make the query, and return a collection of entities, without data-transformers?
Yo @TristanoMilano!
Where does that error come from exactly - what's the stack trace? It might be from this line - https://github.com/api-platform/core/blob/main/src/Symfony/EventListener/WriteListener.php#L116 - but I can't specifically find the
get_class()in the code that follows.If my guess is correct, the thing to do is, from your processor, to return an object, not an array. It seems that returning an object from the processor is required. However, this can be a thin object (it doesn't need to be an ApiResource) that holds your array. Heck, you might even be able to return an
ArrayObject- but I'm no sure.Let me know if this helps - or if I'm way off. In that case, show me the stacktrace :).
Cheers!
It was as easy as that. Instead of returning the ArrayCollection in my Processor, I just returned am ArrayObject and it worked.
Thank you very much!
Booya! Thank you for replying back that this did the trick! Keep up the good work :)
I am still having one little issue with that approach, The response is fine, but how do I tell API-Platform to show the correct Schema? I want to show the same Schema, that is auto-generated with every GetCollection() endpoint, so like this:
[image]
But what I get is an object.
I can show you the example code of my ApiResource:
Again, the response is just fine. In the processor, I query the Sesssion entity for the $eventId (UUID) and the array of programIds (an array of UUIDs) and return an ArrayObject of sessions.
Because it can be a long list of $programIds and that are all UUIDs I can not use Get() but am forced to use Post()'s body.
Is there any way of telling API-Platform to return the Schema of a SessionCollcetion instead of the Schema of a Session-object?
Hey @TristanoMilano!
Hmm. I think this is the job of the
outputoption. Because you haveoutput: Session::class, I would expect that the documentation would show that your post operation returns aSession... and it sounds like that IS what you're seeing.What I'm not sure about is... exactly what you should replace this with... it's not like we can say
output: array<Session::class>...I'm not sure if you'll be able to get it to exactly match the "GET collection" documentation. You could try creating a new class - e.g.
SessionCollectionthat has asessionsproperty... and then set theoutputtoSessionCollection::class. That would probably fix the docs, but it would also change the output (you'd now have asessionkey with the actual data on it.In short, I'm not really sure how to get this to document correctly :/
Thanks for the response. My colleague came up with a solution using the OpenApiFactory to get the Schema we want:
Boom! I love the power that decorating that gives you as a last resort!
Thanks for sharing that :)
"Houston: no signs of life"
Start the conversation!