gpt4 book ai didi

json - Symfony2 Doctrine2 反序列化和合并实体问题

转载 作者:行者123 更新时间:2023-12-04 18:40:10 31 4
gpt4 key购买 nike

我正在尝试将 json 反序列化为实体,然后合并实体。

我相信我过去曾使用过此方法,我会在其中发送 ID 和我希望更新的任何字段。例如:

在我的数据库中:

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

然后我将反序列化以下 json 并合并实体
$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_serializer 有关。或 em->merge .
$customer->getFirst()在实体合并之前和之后返回 null

最佳答案

反序列化器将您的 JSON 字符串转换为一个对象,仅此而已。它将使用您序列化的属性。如果未设置属性,它将保持为 null(或在您的类中指定的默认值)。

合并方法还会将空属性持久化到数据库中。

为避免这种情况,请查看以下答案:how to update symfony2/doctrine entity from a @Groups inclusion policy JMSSerializer deserialized entity

持久化实体后,在实体上调用 EntityManager::refresh() 方法应加载缺少的属性。

还相关:

  • How to update a Doctrine Entity from a serialized JSON?
  • How to manage deserialized entities with entity manager?
  • Doctrine2 ORM Ignore relations in Merge
  • 关于json - Symfony2 Doctrine2 反序列化和合并实体问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28144842/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com