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
}
}
4 Comments
One more question, patch() should update specific data, no? But it seems to replace those two data in the database ( and remove that second one ) but one gets removed (the second one), I mean why should it get removed due to a patch() request? just because we didn't send it during the update
So, is it removing from the database because when deserializing, it's owner gets set to null, because of the data that's sent ( i.e. dragonTreasures => [ '/api/treasures/1', 'api/treasures/2' ] ) does not get deserialized, and as a result, it's owner is missing ?
Hey @sujal_k!
This longer description of the whole process might help :)
https://symfonycasts.com/screencast/api-platform-extending/collection-relation-setting#comment-32177
Cheers!
At 5:26 timestamp, when I ran the test, it says:
I think I followed it correctly, but it says like that.
Till that dump() just before this failing test, it worked fine.
But when inside DragonTreasure entity, when I set nullable: true, the test passed but in database owner is set to NULL for dragon treasure with id=2
Hey @sujal_k!
Hmm. Here is what should be happening, maybe it will help:
1) We start with a
DragonTreasurewithownerset to, let's say userid=2. In the test this user starts with 2 dragon treasures (via lines 61 and 62 of https://symfonycasts.com/screencast/api-platform-extending/collection-relation-setting#codeblock-0bc8575f72).2) In the test, we send a
PATCHrequest to modify user 2. Specifically, we modify thedragonTreasuresproperty. We set it to an array, but only send the IRI of ONE of the two treasures assigned to this user.3) When the serializer sees this, it realizes that this use currently has 2 treasures, but we only included the IRI for 1. It actually realizes that we're removing one treasure from this user. And so, it calls
$user2->removeDragonTreasure($treasure)where$treasureis theDragonTreasureobject whose IRI we did not send.4) This method removes the treasure from the
User.dragonTreasuresproperty. But more importantly, it also calls$treasure->setOwner(null).5) So at this point, your error makes perfect sense: the serializer is setting the
ownertonull. So why do we expect theDragonTreasureto be deleted instead? Because oforphanRemoval: https://symfonycasts.com/screencast/api-platform/collections-remove-item. With that in place, if theownerof aDragonTreasureis set tonull, instead of saving that, Doctrine deletes theDragonTreasureentirely.So perhaps
orphanRemovalis missing. Or some thing in this process is not being translated through correctly when the DTO -> entity mapping logic happens.This isn't an answer, but hopefully it can help your debugging :).
Cheers!
"Houston: no signs of life"
Start the conversation!