0

I'm Using Symfony2 with the DoctrineMongoDB Bundle. I made a service that receives Informations in JSON Format (Objects).

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. 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)

[{"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.

Any Help would be great! Thank you!

Edit:

This is the code that handles the JSON Input

$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();
doggy
  • 1
  • 2
  • Can you share the actual code that is taking the JSON payload, modifying your model, and performing the `flush()`? The JSON doesn't directly match the document structure, so we're still not seeing how your managed documents are being modified. – jmikola Jul 31 '13 at 15:25
  • I have upated my post – doggy Jul 31 '13 at 15:42
  • In your example, it doesn't look as if you're setting a reference. I'm not sure if you're using the Symfony component or [Johannes'](http://jmsyst.com/libs/serializer)) library, but the deserialization is constructing a new objects of each class. ODM the JokePattern gets persisted by the cascade, and JokeEmbedded will only contain a reference when stored in a Fun document. If you have a setter method, the `$title` property on JokeEmbedded may also get set, but I can't tell. I would suggest looking at the serializer docs for excluding fields. – jmikola Jul 31 '13 at 17:19
  • Hello, yes the new reference is not set by code. Its in the JSON and should be managed internaly. That does work but as u now the title gets changed also. So there isnt a away to manage this with doctrine? I'm using jms serializer, any idea how to do that? I need a way to difference the serialize and the deserialize settings - but i cant find something like that in de docs. :( Thx :) – doggy Jul 31 '13 at 18:23
  • I'm goin to play around with the @Accessor/@HandlerCallback and manage it by code in the model. Or directly in the Setter-Method. I will post my status soon :) Thx so far – doggy Jul 31 '13 at 18:27

1 Answers1

0

I managed it with the deserialization context and the list annotation within the JMS SerializerBundle.

Greetings :)

doggy
  • 1
  • 2