Deserializing Child Object with the Symfony Serializer

I am trying to use the Symfony Serializer Component to deserialize a json object. The User has a ManyToOne relationship with Organization:

{
    "name": "John",
    "email": "[email protected]",
    "organization": {
        "id": 2
    }
}

I am then attempting to persist the user entity inside the controller handling the POST request:

$user = $serializer->deserialize($request->getContent(), User::class, 'json');
$entityManager->persist($user);
$entityManager->flush();

However this throws the error:

DoctrineORMORMInvalidArgumentException: A new entity was found through the relationship 'AppEntityUser#organization' that was not configured to cascade persist 

This would lead me to think that the deserializer is not recognizing the existing organization and trying to create a new one?

How can I create a new User with the nested organization using the deserializer?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *