I'm trying to update symfony2/doctrine entities using JMSSerializer with an @ExclusionPolicy:None @Groups Inclusion Policy.
* @SerializerExclusionPolicy("none")
*/
class Foo
{
/**
* @SerializerGroups({"flag","edit"})
*/
protected $id;
/**
* @SerializerGroups({"edit"})
*/
protected $name;
/**
* @SerializerGroups({"flag"})
*/
protected $flag;
/**
* @SerializerExclude()
*/
protected $createdBy;
}
reference: http://jmsyst.com/libs/serializer/master/reference/annotations
the result for the following record:
Foo (id:1, name:'bar', flagged:false ,created_by:123)
is serialized using Group inclusion to avoid serializing information I don't need (associations, blobs, etc..) so when I want to update an entity I deserialize only the updated fields of the entity from the JSON.
$foo->setFlagged(true);
$data = $serializer->serialize($foo, 'json', SerializationContext::create()->setGroups(array("flag")));
result:
{id:1,flagged:true}
which when passed back to the application deserializes into the entity
$foo = $serializer->deserialize($jsonFoo,'Foo','json');
result:
Foo (id:1, name:null, flagged:true, created_by:null)
The problem is when I try to merge the entity back into the doctrine entity manager:
$foo = $em->merge($foo);
$em->persist($foo);
$em->flush();
The resulting foo is trying to update excluded properties (name,created_by) with null.
How do I tell JMSSerializer or Doctrine Entity Managers merge that I don't want to overwrite existing properties with null?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…