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