Been scratching my head with this one. I have a symfony 2 json api that is providing data to an android app.
The api serializes the data and sends it off to the app using the JMSSerializerBundle with no problems at all. I then use GSON to deserialize to native java objects.
My app then makes changes and posts the data back to the api.
My problem is that when I deserialize the json sent from the app and try to persist/merge it new objects are created in the database for entities which already exist. Can symfony 2 determine whether an object is new or not? I would have thought if it had an Id with the record it would know it was to be updated and not created.
Here's my controller code :
public function postSuggestionAction()
{
$content = $this->get("request")->getContent();
if (!empty($content))
{
$serializer = $this->get('jms_serializer');
$suggestion = $serializer->deserialize($content, 'Calling\WebBundle\Entity\Suggestion', 'json');
$em = $this->getDoctrine()->getManager();
$em->persist($suggestion);
$em->merge($suggestion->getCategory());
$em->merge($suggestion->getNumber());
$em->merge($suggestion->getCaller());
$em->flush();
$view = $this->view(true, 200);
return $this->handleView($view);
}
$view = $this->view(false, 404);
return $this->handleView($view);
}
Thanks in advance Steve