简体   繁体   English

原则MongoDB ODM不会更改引用对象的状态

[英]Doctrine MongoDB ODM do not change state of referenced object

I'm Using Symfony2 with the DoctrineMongoDB Bundle. 我正在将Symfony2与DoctrineMongoDB捆绑包一起使用。 I made a service that receives Informations in JSON Format (Objects). 我做了一个接收JSON格式的信息(对象)的服务。

The Object I'm sending got a property for referencing to another Object in a different collection in the Database. 我要发送的对象有一个属性,用于引用数据库中不同集合中的另一个对象。

Changing the reference works. 更改参考作品。 But in case I send another field, like "title" with the ObjectB - it sets the title to the new value in the DataBase. 但是,如果我发送另一个字段,例如带有ObjectB的“ title”,它将在数据库中将标题设置为新值。 How can I prevent this? 我该如何预防?

I just want to set the new reference, no manipulation on that Object what so ever. 我只想设置新的引用,而无需对该对象进行任何操作。

Here is some Code (shortened) 这是一些代码(缩短)

class Fun{    
   /**
     * @MongoDB\Id(strategy="auto")
     */
    private $id;

   /** @MongoDB\EmbedMany(targetDocument="JokeEmbedded", strategy="set")
    */
   private $jokes = array();

}


class JokeEmbedded
{
    /**
     * @MongoDB\ReferenceOne(targetDocument="JokePattern", cascade={"persist"})
     */
    private $ref;

    /**
     * @MongoDB\String
     */
    private $title;
}

class JokePattern
{
    /**
     * @MongoDB\Id(strategy="AUTO")
     */
    private $id;

    /**
     * @MongoDB\String
     */
    private $title;
}

I'm now sending the following JSON to the Service: (JSON represents the ObjetClass Fun) 我现在将以下JSON发送到服务:(JSON代表ObjetClass Fun)

[{"id":"1","jokes":[{"ref":{"id":"222", "title":"new title"}]]

My question is now, how do I ignore the new given "title" for the reference I want to Set? 现在的问题是,我该如何忽略要设置的参考的新给定“标题”? I want to have the new reference in the DB set to the ID 222. Nothing more. 我想在数据库中将新引用设置为ID222。仅此而已。

Any Help would be great! 任何帮助将是巨大的! Thank you! 谢谢!

Edit: 编辑:

This is the code that handles the JSON Input 这是处理JSON输入的代码

$request = $this->getRequest();
//Get JSON-Data
$data = $request->getContent();
$funs = $this->get('serializer')->deserialize(
    $data,
    'ArrayCollection<Acme\FunBundle\Document\Fun>',
    'json'
);

//create documentmanager
$dm = $this->get('doctrine_mongodb')->getManager();

foreach ($funs as $obj) {
        //save to db
        $dm->persist($obj);
    }
$dm->flush();

I managed it with the deserialization context and the list annotation within the JMS SerializerBundle. 我通过反序列化上下文和JMS SerializerBundle中的列表批注对其进行管理。

Greetings :) 问候 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM