本文介绍了如何从@Groups包含策略更新symfony2 / doctrine实体JMSSerializer反序列化实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用带有@ExclusionPolicy的JMSSerializer更新symfony2 / doctrine实体:无@Groups包含策略。

I'm trying to update symfony2/doctrine entities using JMSSerializer with an @ExclusionPolicy:None @Groups Inclusion Policy.

 * @Serializer\ExclusionPolicy("none")
 */
 class Foo
 {
    /**
     * @Serializer\Groups({"flag","edit"})
     */
    protected $id;

    /**
     * @Serializer\Groups({"edit"})
     */
    protected $name;

    /**
     * @Serializer\Groups({"flag"})
     */
    protected $flag;

    /**
     * @Serializer\Exclude()
     */
    protected $createdBy;
 }

参考:

以下记录:

Foo (id:1, name:'bar', flagged:false ,created_by:123)

使用组包含序列化,以避免序列化不需要的信息(关联,blob等)当我想更新一个实体时,我从JSON中反序列化实体的更新字段。

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();

最终的foo正在尝试使用null更新排除的属性(name,created_by)。

The resulting foo is trying to update excluded properties (name,created_by) with null.

如何告诉JMSSerializer或Doctrine实体管理器合并我不想用null替换现有的属性?

推荐答案

我找到答案。

$ serializer 是由symfony2集成包创建的服务 JMSSerializerBundle

默认服务是 jms_serializer.serializer 使用默认的Object Constructor UnserializeObjectConstructor 初始化 JMSSerializer 对于学说,我需要使用 DoctrineObjectConstructor 反序列化。

The default service is jms_serializer.serializer initializes the JMSSerializer with the default Object Constructor UnserializeObjectConstructor and for doctrine I needed to deserialize with the DoctrineObjectConstructor.

因为我只使用 JMSSerializer 在项目中序列化/反序列化原则实体,我覆盖了 JMSSerializerBundle jms_serializer.object_constructor 与正确的对象构造函数的别名。

because I only use JMSSerializer in the project for serialize/deserialize of doctrine entities, I overwrote JMSSerializerBundle's jms_serializer.object_constructor with the alias of the proper object constructor service.

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

有没有更好的方法来配置串行器使用的对象构造函数?

我还添加了反序列化的正确上下文:

I also added the proper context to deserialize:

$serializer->deserialize($jsonFoo,'Foo','json', DeserializationContext::create()->setGroups(array('flag')));

result:
Foo (id:1, name:'bar', flagged:true ,created_by:123)

使用doctrine对象构造函数,它显示我想要找到该对象,并且仅将更新应用于 $ jsonFoo (和标志组)。这完全不需要理论实体管理器合并,我可以正确地保持对象。

Using the doctrine object constructor, it figures out that I want to find the object and only apply updates to fields provided in $jsonFoo (and the flag group). This totally eliminates the need for doctrines entity manager merge and I can just persist the object properly.

$em->persist($foo);
$em->flush();

这篇关于如何从@Groups包含策略更新symfony2 / doctrine实体JMSSerializer反序列化实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 08:48