Escritura incrustada
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 SubscribeVoy a probar la ruta GET one treasure endpoint... utilizando un identificador real. Perfecto. Debido a los cambios que acabamos de hacer, el campo owner está incrustado.
¿Y si cambiamos el propietario? Pan comido: siempre que el campo sea escribible... y el nuestro lo es. Ahora mismo el owner es el id 1. Utiliza la ruta PUT para actualizar el id 2. Para la carga útil, establece owner en /api/users/3.
Y... ¡a ejecutar! ¡Bah! Error de sintaxis. JSON está malhumorado. Elimina la coma, inténtalo de nuevo y... ¡sí! El owner vuelve como el IRI /api/users/3.
Enviar datos incrustados a Update
¡Pero ahora quiero hacer algo salvaje! Este tesoro pertenece al usuario 3. Vamos a obtener sus datos. Abre la ruta GET un usuario, pruébala, introduce 3 y... ¡ahí está! El nombre de usuario es burnout400.
Éste es el objetivo: al actualizar un DragonTreasure -por tanto, al utilizar la ruta PUT a /api/treasures/{id} -, en lugar de cambiar de un propietario a otro, quiero cambiar el username del propietario existente. Algo así: en lugar de establecerowner a la cadena IRI, establecerla a un objeto con username asignado a algo nuevo.
¿Funcionaría? ¡Experimentemos! Pulsa Ejecutar y no funciona. Dice
No se permiten documentos anidados para el atributo
owner, utiliza en su lugar IRI.
Permitir la incrustación de propiedades escribibles
Así que, a primera vista, parece que esto no está permitido: parece que aquí sólo puedes utilizar una cadena IRI. Pero, en realidad, sí está permitido. El problema es que el campousername no es escribible mediante esta operación.
Pensemos en esto. Estamos actualizando un DragonTreasure. Esto significa que API Platform está utilizando el grupo de serialización treasure:write. Ese grupo está por encima de la propiedad owner, por lo que podemos modificar el 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 | |
| } |
Pero si queremos poder cambiar el username del propietario, entonces también tenemos que entrar en User y añadir ese grupo aquí.
| // ... 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 | |
| } |
Esto funciona exactamente igual que los campos incrustados cuando los leemos. Básicamente, como al menos un campo de User tiene el grupo treasure:write, ahora podemos enviar un objeto al campo owner.
Objetos nuevos vs existentes en datos incrustados
Observa: enciéndelo de nuevo. Funciona... casi. Obtenemos un error 500:
Se ha encontrado una nueva entidad a través de la relación
DragonTreasure.owner, pero no se ha configurado para persistir encascade.
Woh. Esto significa que el serializador vio nuestros datos, creó un nuevo objeto User y luego configuró el username en él. Doctrine falló porque nunca le dijimos que persistiera el nuevo objeto User.
Aunque... esa no es la cuestión: ¡la cuestión es que no queremos un nuevo User! Queremos coger al propietario existente y actualizar su username.
Por cierto, para que este ejemplo sea más realista, añadamos también un name a la carga útil para que podamos fingir que estamos actualizando realmente el tesoro... y decidamos actualizar también el username del propietario mientras estamos en el vecindario.
En cualquier caso: ¿cómo le decimos al serializador que utilice el propietario existente en lugar de crear uno nuevo? Añadiendo un campo @id configurado con el IRI del usuario: /api/users/3.
Ya está Cuando el serializador ve un objeto, si no tiene un @id, crea un objeto nuevo. Si tiene @id, encuentra ese objeto y le asigna cualquier dato.
Así pues, llega el momento de la verdad. Cuando lo probamos... por supuesto, otro error de sintaxis. ¡Ponte las pilas, Ryan! Después de arreglarlo... ¡perfecto! ¡Un código de estado 200! Aunque... realmente no podemos ver si actualizó el username aquí... ya que sólo muestra el propietario.
Utiliza la ruta GET one User... busca al usuario 3... ¡y comprueba esos dulces datos! Sí cambió el username.
Vale, me doy cuenta de que este ejemplo puede no haber sido el más realista, pero poder actualizar objetos relacionados tiene muchos casos de uso reales.
Utilizar Persistir en cascada para crear un nuevo objeto
Volviendo a la petición de PUT, ¿qué pasaría si quisiéramos crear y guardar un nuevo objetoUser? ¿Es posible? Pues sí
En primer lugar, tendríamos que añadir un cascade: ['persist'] al atributo treasure.ownerORM\Column . Esto es algo que veremos más adelante. Y en segundo lugar, tendríamos que asegurarnos de exponer todos los campos obligatorios como escribibles. Ahora mismo sólo username es escribible... así que no podríamos enviar password o email.
La restricción válida
Antes de continuar, nos falta un pequeño, pero importante, detalle. Intentemos esta actualización una vez más con el @id. Pero establece username en una cadena vacía.
Recuerda que el campo username tiene un NotBlank encima, por lo que debería fallar la validación. Y sin embargo, cuando lo intentamos, ¡obtenemos un código de estado 200! Y si vamos a la ruta GET de un usuario... sí, ¡el username ahora está vacío! Eso es... un problema.
¿Cómo ha ocurrido? Por cómo funciona el sistema de validación de Symfony.
La entidad de nivel superior -el objeto que estamos modificando directamente- es DragonTreasure. Así que el sistema de validación mira DragonTreasure y ejecuta todas las restricciones de validación. Sin embargo, cuando llega a un objeto como la propiedad owner, se detiene. No sigue validando también ese objeto.
Si quieres que eso ocurra, tienes que añadir una restricción a esto llamadaAssert\Valid.
| // ... lines 1 - 55 | |
| class DragonTreasure | |
| { | |
| // ... lines 58 - 100 | |
| private ?User $owner = null; | |
| // ... lines 103 - 214 | |
| } |
Ahora... en nuestra ruta PUT... si lo intentamos de nuevo, ¡sí! 422: owner.username, este valor no debe estar en blanco.
La posibilidad de actualizar un objeto incrustado es muy útil y potente. Pero el coste de esto es hacer que tu API sea cada vez más compleja. Así que, aunque puedes optar por hacer esto -y deberías hacerlo si es lo que quieres-, también podrías optar por obligar al cliente de la API a actualizar primero el tesoro... y luego hacer una segunda petición para actualizar el nombre de usuario del usuario... en lugar de permitirle que lo haga todo de lujo al mismo tiempo.
A continuación: veamos esta relación desde el otro lado. Cuando estamos actualizando un User, ¿podríamos actualizar también los tesoros que pertenecen a ese usuario? ¡Averigüémoslo!
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!