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
}
}
12 Comments
Hey Ryan,
you might say it already but i can't find the reference in your course, if i execute a patch request with a collection of embed objects (not IRI) , there is a way to use the EntityClassDtoStateProcessor or should i use a custom processor class ?
or should i use the serialization group in ApiResource class like we used to do it entity class ?
Hey @Joel-L!
You're doing something like PATCH:
Right? Without thinking about this too hard, yes, the answer should be to use serialization groups. This all comes down to the serializer: we want it to "accept" the serialization groups and return them into DTO objects. Then, by the time the code gets to our state processor, we should have (to use my example here) a
ProductDtowith aspecsproperty full ofProductSpecDtoobjects. Then, transforming that into entity objects and saving them shouldn't be any different.The biggest bummer for me when allowing embedded objects with this setup is that you suddenly do need to use serialization groups. The system simplified life so that we didn't need them anymore... but if you want to accept embedded data, then you do need them.
Cheers!
Thanks Ryan
in that direction i'm facing a new problem :
There is a Product and a (child) Item entity class.
I've created a ProductApi and ItemApi resource class everything is fine except for Patch operation who i got the error above.
It seems happening from the mapper
$this->propertyAccessor->setValue($entity, 'items', $importItemEntities);where $entity is ProductApi and items is ItemApi Collection.
Any idea ?
Hey @Joel-L!
On your
Productclass, how is the$itemsproperty accessible? You would typically have anaddItem()andremoveItem()method, and the property accessor is smart enough to call those. Do you have them (they're added by MakerBundle if you generated the relationship viamake:entityand said that you wanted to map the "inverse" side of the relation),Cheers!
yes i have them :
getItems()
addItem()
removeItem()
Hmm, then that just doesn't make sense. The error specifically is:
So this means that PropertyAccess component is being asked to read or write the "items" property on the
Productentity. And being able to usegetItems()(in the case of reading) oraddItem()/removeItem()is core to the property accessor component https://symfony.com/doc/current/components/property_access.html#writing-to-array-propertiesHmm, unless there is something weird happening because
itemson your entity is aCollectionand on your DTO it is an array. You could try making youritemson your DTO anArrayCollectionfrom Doctrine to test that theory (so that both sides are a type of "collection" object).Cheers!
below the source code i'm using , jus tin case you have time :
Product entity
Item entity
ProductApi resource
ItemApi resource
ProductApiToEntityMapper
It all looks fine. My best guess, of something to try - just to see if it makes a difference - would be to make the
itemsproperty inProductApian array, instead of anArrayCollection. That shouldn't make any difference, but your entity looks fine. So the idea is that: perhaps the property accessor is getting confused by theArrayCollection(not realizing it is a "collection") and so is not looking for an adder/remove and is instead only looking for a setter.Cheers!
thanks for your time.
But unfortunately i got same error message.
I will still searching
i found the issue it was mispelling items name
fix now with Collection everywhere
thks againe for your time
Phew! Fantastic - glad you got it sorted.
Happy holidays!
"Houston: no signs of life"
Start the conversation!