Chapters
28 Chapters
|
2:51:42
|
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.2",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^3.0", // v3.0.8
"doctrine/annotations": "^1.0", // 1.14.2
"doctrine/doctrine-bundle": "^2.8", // 2.8.0
"doctrine/doctrine-migrations-bundle": "^3.2", // 3.2.2
"doctrine/orm": "^2.14", // 2.14.0
"nelmio/cors-bundle": "^2.2", // 2.2.0
"nesbot/carbon": "^2.64", // 2.64.1
"phpdocumentor/reflection-docblock": "^5.3", // 5.3.0
"phpstan/phpdoc-parser": "^1.15", // 1.15.3
"symfony/asset": "6.2.*", // v6.2.0
"symfony/console": "6.2.*", // v6.2.3
"symfony/dotenv": "6.2.*", // v6.2.0
"symfony/expression-language": "6.2.*", // v6.2.2
"symfony/flex": "^2", // v2.2.4
"symfony/framework-bundle": "6.2.*", // v6.2.3
"symfony/property-access": "6.2.*", // v6.2.3
"symfony/property-info": "6.2.*", // v6.2.3
"symfony/runtime": "6.2.*", // v6.2.0
"symfony/security-bundle": "6.2.*", // v6.2.3
"symfony/serializer": "6.2.*", // v6.2.3
"symfony/twig-bundle": "6.2.*", // v6.2.3
"symfony/ux-react": "^2.6", // v2.6.1
"symfony/validator": "6.2.*", // v6.2.3
"symfony/webpack-encore-bundle": "^1.16", // v1.16.0
"symfony/yaml": "6.2.*" // v6.2.2
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.2
"symfony/debug-bundle": "6.2.*", // v6.2.1
"symfony/maker-bundle": "^1.48", // v1.48.0
"symfony/monolog-bundle": "^3.0", // v3.8.0
"symfony/stopwatch": "6.2.*", // v6.2.0
"symfony/web-profiler-bundle": "6.2.*", // v6.2.4
"zenstruck/foundry": "^1.26" // v1.26.0
}
}
4 Comments
Hello, I have two questions. 1) Is it possible to filter on enum property? 2) How can I filter on a non-persisted property in my entity? I have other filters such as Boolean added as services and then passed in as filters array on a one of my collections (because I have two collections and I want the filters on one but not the other). Thank you in advance.
Hiya @Brent-M!
I'm actually not sure! I'm sure you could create a custom entity filter for this - https://symfonycasts.com/screencast/api-platform2-extending/entity-filter-logic - but I also can't find any info one way or another on if enum property filter support is there. I would give it a try... though I'm guessing you already have :)
In general, when you create a custom filter - https://symfonycasts.com/screencast/api-platform2-extending/entity-filter-logic - you're free to create filters that use any name - e.g.
foobar=baz. So, I think you could create a custom filter for our non-persisted property - e.g.?nonPersistedProp=foo. The trick is that, in your custom filter, you then need to somehow take this value -foo- and modify the query to only include the items that match. For a non-persisted field, sometimes it still may be obvious how you could modify the query for that. But in other cases, that non-persisted field is so custom & "odd", that it's not really possible to modify the query to return only the correct records.If you have that case, I'm less sure. But the flow goes like this:
A) The collection provider is called to fetch all of the object for
/products, for example.B) Internally (assuming your
ApiResourceis an entity), this will trigger the "doctrine extensions" system in API Platform... which includes the filters. In short, your custom filter will be called. But, if you aren't able to modify the query at this point.... you're stuck.C) The collection provider executes the query and returns the result.
So, if you can't modify the query, your best situation might be to decorate the collection provider, allow it to execute and return the results, and then to loop over those results, remove the ones you don't want, and return the rest. This can get trickier with pagination, as the collection provider isn't returning just 25 Products, it's returning a Paginator with 25 objects in it. But we do something similar in one tutorial - https://symfonycasts.com/screencast/api-platform-extending/pagination#codeblock-da08790a13 - we loop over the results in the paginator, then put the final results into our own paginator.
Let me know if any of this help!
Cheers!
Hey,
It's is possible to make preload request in PHP-Symfony ? I mean in one request get all Collections inside Collection - like in your example From Book API request get author data, purchase data of this book and so on
Regards
Hey @Mepcuk!
Oh, that's an excellent question / point. Yes, check out https://api-platform.com/docs/core/push-relations/ - let me know if that's what you're looking for.
Cheers!
"Houston: no signs of life"
Start the conversation!