Chapters
48 Chapters
|
5:13:28
|
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!
09.
Completely Custom Field via a Data Provider
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.
This tutorial also works great with API Platform 2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": ">=8.2",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.5.10
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.12.1
"doctrine/doctrine-bundle": "^2.0", // 2.1.2
"doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.2
"doctrine/orm": "^2.4.5", // 2.8.2
"nelmio/cors-bundle": "^2.1", // 2.1.0
"nesbot/carbon": "^2.17", // 2.39.1
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0 || ^5.0", // 5.2.2
"ramsey/uuid-doctrine": "^1.6", // 1.6.0
"symfony/asset": "5.1.*", // v5.1.5
"symfony/console": "5.1.*", // v5.1.5
"symfony/debug-bundle": "5.1.*", // v5.1.5
"symfony/dotenv": "5.1.*", // v5.1.5
"symfony/expression-language": "5.1.*", // v5.1.5
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "5.1.*", // v5.1.5
"symfony/http-client": "5.1.*", // v5.1.5
"symfony/monolog-bundle": "^3.4", // v3.5.0
"symfony/security-bundle": "5.1.*", // v5.1.5
"symfony/twig-bundle": "5.1.*", // v5.1.5
"symfony/validator": "5.1.*", // v5.1.5
"symfony/webpack-encore-bundle": "^1.6", // v1.8.0
"symfony/yaml": "5.1.*" // v5.1.5
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.3", // 3.3.2
"symfony/browser-kit": "5.1.*", // v5.1.5
"symfony/css-selector": "5.1.*", // v5.1.5
"symfony/maker-bundle": "^1.11", // v1.23.0
"symfony/phpunit-bridge": "5.1.*", // v5.1.5
"symfony/stopwatch": "5.1.*", // v5.1.5
"symfony/twig-bundle": "5.1.*", // v5.1.5
"symfony/web-profiler-bundle": "5.1.*", // v5.1.5
"zenstruck/foundry": "^1.1" // v1.8.0
}
}
10 Comments
Hi,
In Api Platform 3 there is no
How to handle this Custom Field via Data Provider?
The way I see it, in Api Platform 3 you are supposed to use StateProvider, implementing ProviderInterface. See https://api-platform.com/docs/core/state-providers/ for more. As I am trying to learn things myself, this might not be correct.
Hey Patrick!
I'm actually not sure myself - sorry I can't be of more assistance! Updating the tutorials for API Platform 3 is my December project right after SymfonyCon :). But yes, I believe you are correct. In fact, if you try to go to https://api-platform.com/docs/core/data-providers/ it redirects you to the page about state-providers :).
Cheers!
Hello,
I have a custom field in my File entity which i set in my DataProvider. This field can only be read and by default it is null and while it does show correctly on my File resource, it doesn't show in a response of a different resource that has a relation to the File entity. For example, the File entity is a OneToMany to a ProductFile join table which also has a relation of ManyToOne to Product Entity. When i make a GET request to Product resource i get a list of products each with a productFiles array of files objects and in this object the custom field is missing. After some debugging it looks like is missing because apiplatform doesn't display null properties by default, so the problem is that my custom field isn't set.. actually the File data provider isn't used when the file is a sub resource of a another resource. Any ideas?
Hey Petru L. !
Sorry for my VERY late reply - your question was left for me and I've been deep in tutorial land :).
Ok, I think I can answer this :). Here's the key:
The "data provider" system is only called ONCE per request for the TOP level resource only. So when you request directly for a "File" resource, it's called and everyone is happy :). But if you request to a Product resource, in order to get the "productFiles" field, API Platform simply calls
$product->getProductFiles(), which uses 100% Doctrine logic. Then, for each related file, it does the same thing, calling$productFile->getFile(), once again using normal Doctrine logic and NOT the data provider.This is definitely a gotcha... but I can't remember if I mention in it in the tutorial or not (I hope I do somewhere!).
Anyways, the fix - if you need it to show up in these embedded situations as well - is probably to use a Doctrine listener - like we do here - https://symfonycasts.com/screencast/api-platform-extending/post-load-listener. Other solutions involve adding a listener - https://symfonycasts.com/screencast/api-platform-extending/set-via-listener - or other data provider to set this. But in both of those situations, you would need to have some logic that says "Oh! The main object is a Product. I know that this will include a File, so let me loop over the $product->getProductFiles(), then get each File object, and then set the property". That's not a huge deal - but if you ever had a File that was included as an embedded resource in yet another resource, you'd need to update the logic to handle THAT spot too.
Let me know what you think :). I tend to limit my "resource embedding" to avoid this sort of issue. But obviously, sometimes embedding data is exactly what you want in your API. And we CAN make this work.
Cheers!
Hey weaverryan, no worries, thank you for the reply.
I do get what your saying and in the meantime i end up using an output dto which works for embedded data, haven't tried with more than 1 level though. So i guess i'll just leave it like that until i face another complication.
Also, this Disqus comment system has some real problems lately... issues with suggestions where it keeps the pre suggestion string along with the completion, tags that are not correctly rendered...
Hey Petru L.
Thanks for your input about Disqus. We're aware of it's far from perfect, and it's on our goal to switch to a different system but I can't give any estimation about it
Cheers!
Hi All
When I am testing, always shows
<blockquote>
{"error":"Invalid credentials."}
</blockquote>
And I cannot test, just watching videos as demo
I did copy and paste all code but no result
Hey Kakha K.
That's a bit odd. I believe your requests are been processed in the "dev" environment. If you're using Symfony CLI to spin-up your web server, then start it in this way and give it a try
APP_ENV=test symfony serve -dCheers!
"Houston: no signs of life"
Start the conversation!