Take the 2-minute tour ×

We are using Symfony2 to create an API. When updating a record, we expect the JSON input to represent a serialized updated entity. The JSON data will not contain some fields (for instance, CreatedAt should be set only once when the entity is created - and never updated). For instance, here is an example JSON PUT request:


Here is the PHP code on the Controller that should update the entity according to the JSON above (we are using JMS serializer Bundle):

$supplier = $serializer->deserialize(

The EntityManger understands (correctly) that this is an update request (in fact, a SELECT query is implicitly triggered). The EntityManager also guess (not correctly) that CreatedAt property should be NULLified - it should instead keep the previous one.

How to fix this issue?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

I would use the Doctrine\ORM\Mapping\ClassMetadata API to discover existing fields in your entity. You can do following (I don't know how JMSSerializerBundle works):

//Unserialize data into $data
$metadata = $em->getMetadataFactory()->getMetadataFor($FQCN);
$id = array();
foreach ($metadata->getIdentifierFieldNames() as $identifier) {
    if (!isset($data[$identifier])) {
        throw new InvalidArgumentException('Missing identifier');
    $id[$identifier] = $data[$identifier];
$entity = $em->find($metadata->getName(), $id);
foreach ($metadata->getFieldNames() as $field) {
    //add necessary checks about field read/write operation feasibility here
    if (isset($data[$field])) {
        //careful! setters are not being called! Inflection is up to you if you need it!
        $metadata->setFieldValue($entity, $field, $data[$field]);
share|improve this answer

using the JMSSerializerBundle follow the install instructions at http://jmsyst.com/bundles/JMSSerializerBundle

either create your own serializer service or alter the JMSSerializerBundle to use the doctrine object constructor instead of the simple object constructor.

<service id="jms_serializer.object_constructor" alias="jms_serializer.doctrine_object_constructor" public="false"/>

This basically handles exactly what Ocramius solution does but using the JMSSerializerBundles deserialize.

share|improve this answer

Your Answer


