I am implementing REST Api with a FOSRestBundle, and I ran into a problem with modifying an existing object (PUT)
I have a Student class with ManyToOne relation
/**
* @JMS\MaxDepth(2)
* @ORM\ManyToOne(targetEntity="ClassRoom", inversedBy="students")
* @ORM\JoinColumn(name="classroom_id", referencedColumnName="id")
*/
protected $classRoom;
When I perform the PUT action, I get only the value attributes, since I do not want the user to change the relationship through the put request. This is an example of the data received.
{
"id": 3,
"name": "pelayo",
"second_name": "ramon",
"last_name": "fernandez",
"birthday": "1983-08-15T00:00:00+0200"
}
The data is deserialized using a JMS serializer that sets the $ classRoom attribute to zero because it did not find it in the received data.
When performing a merge
$student2 = $this->get('doctrine')->getManager()->merge($student);
If student2 is saved, the current relationship with the classRoom is deleted from the database, since the merge sets the relation to null.
This behavior can be evaded by fetching the current Room class and binding it to a deserialized object manually before merging, but this is ugly.
?