Symfony2 Doctrine2 De-serialize and Merge Entity

I am trying to de-serialize json into Entity and then merge Entity.

I believe that I had this in the past when I sent the identifier and any fields that I wanted to update. For instance:

In my DB:

| id |  first  | last  |   city   |
|  1 |  Jimmy  | James | Seattle  |

Then I de-serialize the next json and merge the entity

$json = { "id" : 1, "city": "chicago"}
$customer = $serializer->deserialize($json, 'App\CustomerBundle\Entity\Customer', 'json');
$em->merge($customer);

Expected Result:

| id |  first  | last  |   city   |
|  1 |  Jimmy  | James | Chicago  |

However, I get the following:

| id |  first  | last  |   city   |
|  1 |  null   | null  | Chicago  |

As I said, I believe that at some point this worked, I'm not sure if this is due to jms_serializeror em->merge.

$customer->getFirst() returns null Before and after combining an object

+4
source share
3 answers

Deserializer JSON , . , . , ( , ).

null .

, : symfony2/doctrine @Groups JMSSerializer

, , EntityManager:: refresh() .

:

+5

Doctrine merge . , , , . Doctrine:

( ) EntityManager, . EntityManager, EntityManager # merge ($ entity). , .

: http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#merging-entities

, , $customer .

+1

Not very elegant, but I think it will do its job.

$customer = $em->getRepository('CustomerBundle:Customer')
            ->findOneById($jsonParsedId);
if ($customer) {
    $customer->setCity($jsonParsedCity);
    $em->persist($customer);
    $em->flush();
}
0
source

All Articles