Symfony2 Doctrine2反序列化和合并实体问题 [英] Symfony2 Doctrine2 De-Serialize and Merge Entity issue

查看:127
本文介绍了Symfony2 Doctrine2反序列化和合并实体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将json反序列化为一个Entity,然后合并该Entity.

I am trying to de-serialize json into an Entity and then Merge the Entity.

我相信我过去曾在这里工作过,我会发送ID和我想更新的任何字段.例如:

I believe I had this working in the past where I would send the ID and any fields I wished to update. For example:

在我的数据库中:

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

然后我将反序列化以下json并合并实体

I would then de-serialize the following json and merge the entity

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

预期结果将是:

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

但是我得到以下信息:

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

就像我说过的那样,我相信我在某个时候可以进行此工作,我不确定这是否与jms_serializerem->merge有关.

Like I said I believe I had this working at some point, I am unsure if this is related to the jms_serializer or em->merge.

$customer->getFirst()在合并实体之前和之后返回null

$customer->getFirst() returns null Before and After the entity is Merged

推荐答案

反序列化器将JSON字符串转换为对象,仅此而已.它将使用您序列化的属性.如果未设置属性,则该属性将保留为空(或类中指定的默认值).

The deserializer transforms your JSON string into an object, nothing more. It will use the properties you serialized. If a property is not set, it will remain null (or the default value specified in your class).

merge方法还将空属性保留到数据库中.

The merge method will also persist null properties to database.

为避免这种情况,请查看以下答案:

To avoid that, look at the answer from : how to update symfony2/doctrine entity from a @Groups inclusion policy JMSSerializer deserialized entity

在保留实体之后,在实体上调用EntityManager :: refresh()方法应加载缺少的属性.

After you have persisted your entity, calling EntityManager::refresh() method on your entity should load missing properties.

也相关:

  • How to update a Doctrine Entity from a serialized JSON?
  • How to manage deserialized entities with entity manager?
  • Doctrine2 ORM Ignore relations in Merge

这篇关于Symfony2 Doctrine2反序列化和合并实体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆