Embedded Write
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.
With a Subscription, click any sentence in the script to jump to that part of the video!
Login SubscribeI'm going to try out the GET one treasure endpoint... using a real id. Perfect. Because of the changes we just made, the owner field is embedded.
What about changing the owner? Piece of crumb cake: as long as the field is writable... which ours is. Right now the owner is id 1. Use the PUT endpoint to update id 2. For the payload, set owner to /api/users/3.
And... execute! Bah! Syntax error. JSON is crabby. Remove the comma, try again and... yes! The owner comes back as the IRI /api/users/3.
Sending Embedded Data to Update
But now I want to do something wild! This treasure is owned by user 3. Let's go get their details. Open the GET one user endpoint, try it out, enter 3 and... there it is! The username is burnout400.
Here's the goal: while updating a DragonTreasure - so while using the PUT endpoint to /api/treasures/{id} - instead of changing from one owner to another, I want to change the existing owner's username. Something like this: instead of setting owner to the IRI string, set it to an object with username assigned to something new.
Would that work? Let's experiment! Hit Execute and it does not. It says:
Nested documents for attribute
ownerare not allowed, use IRI instead.
Allowing Writable Properties to be Embedded
So, at first glance, it looks like this isn't allowed: it looks like you can only use an IRI string here. But actually, this is allowed. The problem is that the username field is not writable via this operation.
Let's think about this. We're updating a DragonTreasure. This means that API Platform is using the treasure:write serialization group. That group is above the owner property, which is why we can change the owner.
| // ... lines 1 - 25 | |
| ( | |
| // ... lines 27 - 49 | |
| denormalizationContext: [ | |
| 'groups' => ['treasure:write'], | |
| ], | |
| // ... line 53 | |
| ) | |
| // ... line 55 | |
| class DragonTreasure | |
| { | |
| // ... lines 58 - 99 | |
| (['treasure:read', 'treasure:write']) | |
| private ?User $owner = null; | |
| // ... lines 102 - 213 | |
| } |
But if we want to be able to change the owner's username, then we also need to go into User and add that group here.
| // ... lines 1 - 22 | |
| class User implements UserInterface, PasswordAuthenticatedUserInterface | |
| { | |
| // ... lines 25 - 46 | |
| (['user:read', 'user:write', 'treasure:item:get', 'treasure:write']) | |
| // ... line 48 | |
| private ?string $username = null; | |
| // ... lines 50 - 170 | |
| } |
This works exactly like embedded fields when we read them. Basically, since at least one field in User has the treasure:write group, we are now allowed to send an object to the owner field.
New vs Existing Objects in Embedded Data
Watch: fire it up again. It works... almost. We get a 500 error:
A new entity was found through the relationship
DragonTreasure.owner, but was not configured tocascadepersist.
Woh. This means that the serializer saw our data, created a new User object and then set the username onto it. Doctrine failed because we never told it to persist the new User object.
Though... that's not the point: the point is that we don't want a new User! We want to grab the existing owner and update its username.
By the way, to make this example more realistic, let's also add a name to the payload so we can pretend that we're actually updating the treasure... and decide to also update the username of the owner while we're in the neighborhood.
Anyways: how do we tell the serializer to use the existing owner instead of creating a new one? By adding an @id field set to the IRI of the user: /api/users/3.
That's it! When the serializer sees an object, if it does not have an @id, it creates a new object. If it does have an @id, it finds that object and then sets any data onto it.
So, moment of truth. When we try it... of course, another syntax error. Get it together Ryan! After fixing that... perfect! A 200 status code! Though... we can't really see if it updated the username here... since it just shows the owner.
Use the GET one User endpoint... find user 3... and check that sweet data! It did change the username.
Ok, so I realize that this example may not have been the most realistic, but being able to update related objects does have plenty of real use-cases.
Cascading the Persist to Create a new Object
Looking back at that PUT request, what if we did want to allow a new User object to be created and saved? Is that possible? It is!
First, we would need to add a cascade: ['persist'] to the treasure.owner ORM\Column attribute. This is something we'll see later. And second, we would need to make sure to expose all of the required fields as writable. Right now only username is writable... so we couldn't send password or email.
The Valid Constraint
Before we keep going, we are missing one small, but important, detail. Let's try this update one more time with the @id. But set username to an empty string.
Remember, the username field has a NotBlank above it, so this should fail validation. And yet, when we try it, we get a 200 status code! And if we go to the GET one user endpoint... yeah, the username is now empty! That's... a problem.
How did that happen? Because of how Symfony's validation system works.
The top-level entity - the object that we're modifying directly - is DragonTreasure. So the validation system looks at DragonTreasure and it executes all of the validation constraints. However, when it gets to an object like the owner property, it stops. It does not continue to validate that object as well.
If you want that to happen, you need to add a constraint to this called Assert\Valid.
| // ... lines 1 - 55 | |
| class DragonTreasure | |
| { | |
| // ... lines 58 - 100 | |
| private ?User $owner = null; | |
| // ... lines 103 - 214 | |
| } |
Now... on our PUT endpoint... if we try this again, yep! 422: owner.username, this value should not be blank.
Being able to update an embedded object is really neat & powerful. But the cost of this is making your API more and more complex. So while you can choose to do this - and you should if it's what you want - you might also choose to force the API client to update the treasure first... and then make a second request to update the user's username... instead of allowing them to do it all fancy at the same time.
Next: let's look at this relationship from the other side. When we're updating a User, could we also update the treasures that belong to that user? Let's find out!
25 Comments
Hi Ryan,
Thank you so much for yet another fantastic course!
While coding along with the course, I'm now struggling with changing the owner's
username. Multiple issues / questions to that:#[Assert\NotBlank()]annotation prevents updating theUserobject without providing thedescription. Which is actually correct. But how does this work for you? I checked out the code: MyUser#description's annotations appear to be identical to yours.PUTreplaces the object, which means: It removes the current one and creates another one. Which is a correct behaviour as well. But also here: How does it work for you? :)PUTand notPATCHin this case?Best,
Ilya
Hey Automatix,
Hm, I don't see the User object has a description field in this video. Are you talking about a different video? Could you link me?
1) But it depends on what you update. If you update User object - that makes sense you have validation error if your User#description field has
#[Assert\NotBlank()]. But if you update a different object that has relation to User - that may require#[Assert\Valid()]constant on the$userproperty, as it's mentioned in this video for$ownerproperty.2) yes, it may end up into a DB error if your field is not nullable in the DB. To make it work, you should set the field to an empty string at least and then it should not be a DB error. It depends on which behavior you want. Also, sometimes, it may depend on the DB SQL server or DB server version.
3) Mostly because PUT and PATCH operations in the ApiPlatform that is used in this video works identical, i.e. no difference. Since ApiPlatform v4 they indeed work different, so if you don't want to set all the fields that are not specified in the PUT request to NULL on the request - use PATCH or specify the field explicitly :) See the note in this section https://symfonycasts.com/screencast/api-platform/operations#put-vs-patch for more details.
I hope that helps!
Cheers!
Hi,
I'm trying to figure out how to prevent existing embedded entities from being updated in a POST request. However, new entities should be able to be created.
To stay with the example from the cast.
If I create a new DragonTreasure via POST then
POST /api/treasuresPOST /api/treasuresPOST /api/treasuresI haven't yet found a good way to differentiate between creating and updating a nested entity without implementing various things myself.
What is the correct way in API Platform to implement this?
Hey @rabbar!
I can see that you understand the mechanism of how this all works well! To summarize:
Is that accurate? If so, I think it makes sense. But in Api Platform's eyes, these are all just valid ways to change data. I'm not entirely sure how to handle this. The closest I got was to allow updating, but to add a validation check that the API client does or doesn't have permission to update this user. This is not exactly the same, but it's related and can maybe offer some inspiration: https://symfonycasts.com/screencast/api-platform-extending/simpler-validator
Let me know if you have any luck, I'd love to know!
Cheers!
Yes.
I followed your advice with the validator. It works so far.
I have left out the checking of relation collections for now because it doesn't play a role in my case and would only make things unnecessarily complicated.
hi,
why does the other way (from user perspective) not working?
PATCH /api/users/1Result:
Does it only work for OneToMany Relations?
Hey @mjk!
Sorry for my very slow reply! I can see what you want to do and it makes sense. This may be related and help: https://github.com/api-platform/core/issues/5559#issuecomment-1518595331 - let me know if it does!
Cheers!
At 4:06 Timestamp,
I think in my code , sending the data same as below also works , without passing @id as mentioned in the video, and one more thing Is that it seems not working when I'm using PUT, but when using PATCH, it works, could you please explain why ?
Good morning everyone,
First of all, thank you for the great tutorial :)
I'm trying to change the username via the Treasure entity in the User entity.
PUT https://dev.symfonycast-api-start.wip/api/treasures/2
It shows as an error that in Treasure the name and description fields cannot be empty. I thought PUT only changed the fields that were passed in the payload?
Historically API Platform would essentially treat PUT operations as PATCH operations and be used for partially updating resources. I believe in latest versions of API Platform the PUT operation now actually acts like a true PUT, where where all non-specified fields are removed (set to null) and PATCH is the method you should use if you want to partially update a resource by specifying only the fields you want to change.
tldr; when these videos use PUT you should use PATCH instead.
Hey @creativx007
As @opalint mentioned, ApiPlatform changed how they used to treat PUT operations, we have a little warning note in this chapter https://symfonycasts.com/screencast/api-platform/serializer?playAt=269#adding-a-virtual-textdescription-field
Hello!
I'm having issues with a case like the one in this chapter, but with a special feature.
I have a Contract entity that has a OneToMany relationship with Condition.
There are several entities that inherit from Condition: CityCondition, DataCondition, etc.
When I try to post a Contract embedding, for example, CityCondition, it gives an error because it's trying to instantiate the abstract class Condition, even though I'm specifying through IRI that it's a CityCondition.
Any ideas? Is this not allowed, or am I missing something?
Thanks!
Posted JSON
Error:
Hey @Oleguer-C!
Hmmm. I don't have any experience with Doctrine inheritance and the serializer. To debug this, I'd first try to get a stacktrace of where this error comes from. Then maybe we can work backwards from the source to see if / how you could tell the serializer that you want a
CityConditionobject specifically (passing the@typewas a good idea... but apparently isn't enough!).Cheers!
Thank you very much!
After managing to decorate the AbstractItemNormalizer class to use the '@type' field, I realized that it actually works by default, and the problem was that I hadn't configured the 'typeProperty' field in the 'DiscriminatorMap' annotation on the abstract class.
I'm attaching the result in case it can be useful to someone.
Cheers!
Fantastic! Thank you for sharing that!
Hi guys but what's about the case when we have one-one relation, for example User-Profile.
We create a user with a profile, But the profile scope requires iri of the user /api/user/??? It works only if I have IRI user id. All relations has
cascade: ['persist']idea how to solve it?Hey,
IIRC if you are persisting a new object you don't need to set back relation to it, I mean you User payload should be like
That should be enough to create related objects at once.
Cheers!
Hey. Cool series.
I noticed a small issue
After successful update of
username, propertynameof Treasure remained the same.I can say even more. PUT request
has response code:200, but
nameis not changed.For some reason I was missing
setNameinDragonTreasureMystery solved ))
oh heck yeah, thanks! Was having the same thing and it was stumping me.
Since I was dealing with PATCH instead of PUT because of the recent api-platform changes I was looking into that direction.
but setName() was removed when we set it in the constructor. The tutorial made it - on purpose - only settable once during creation. You get no error when you do try to change it later though.
Again, thanks, was pulling my hear out!
(Didn't even try it yet but the moment I read your comment the lightbulb went off in my head :wink:)
Hey Joris-Mak,
Thanks for mentioning that was a useful comment for you!
Cheers!
Hey @seshy,
Awesome news! Such things happens sometimes! Keep going!
Cheers!
When I want to change username and name of treasure I receive error:
"hydra:description": "name: Describe your loot in 50 chars or less\ncoolFactor: This value should be less than or equal to 10.",I know that is setted #[Assert\NotBlank] but it's just the same like you have code?
Hey @Szymon!
It looks like the data on that treasure is STARTING invalid - like the coolFactor in the database is somehow already greater than 10. And so, even though you’re not changing that field, it fails validation. I think for the treasure’s name I may have a bug on the fixtures that randomly sets a name that’s longer than 50 - so that is likely the cause of that error. I might (?) have some problem also with coolFactor!
So, just a data error on the database - and probably my fault. On production, thanks to validation, that data would never be able to get into this invalid state.
Cheers!
Yeah, that is the reason, changing the
getDefaults()inDragonTreasureFactory.phpto something like:`protected function getDefaults(): array
will fix it.
Regards!
"Houston: no signs of life"
Start the conversation!