I'm implementing a REST Api with FOSRestBundle and I have encountered a problem with the modification of existing entity (PUT)
I have a class Student with has a ManyToOne relation
/**
* @JMS\MaxDepth(2)
* @ORM\ManyToOne(targetEntity="ClassRoom", inversedBy="students")
* @ORM\JoinColumn(name="classroom_id", referencedColumnName="id")
*/
protected $classRoom;
When performing a PUT action I only receive the value attributes since i do not want to let the user modify the relations via a put request. This is a received data example.
{
"id": 3,
"name": "pelayo",
"second_name": "ramon",
"last_name": "fernandez",
"birthday": "1983-08-15T00:00:00+0200"
}
Data gets deserialized with JMS serializer wich sets the $classRoom attribute to null since it didn't find it in the received data.
When performing a merge
$student2 = $this->get('doctrine')->getManager()->merge($student);
If the student2 gets persisted the current relation with classRoom gets erased from the database since the merge sets the relation to null.
This behavior can be dodged by retrieving the current classRoom and setting it to the deserialized entity by hand before the merge, but this is ugly.
Is there any way to tell doctrine to ignore an attribute in a merge from the detached one and make it always use the stored value?