{"id":1611,"date":"2022-08-30T15:17:58","date_gmt":"2022-08-30T15:17:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/doctrine-mongo-odm-merge-for-externally-modified-data-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:58","modified_gmt":"2022-08-30T15:17:58","slug":"doctrine-mongo-odm-merge-for-externally-modified-data-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/doctrine-mongo-odm-merge-for-externally-modified-data-collection-of-common-programming-errors\/","title":{"rendered":"Doctrine Mongo ODM merge for externally modified data-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m writing a Symfony2 app that allows mobile users to create and update &#8220;Homes&#8221; via a REST service. I&#8217;m using MongoDB as the storage layer and Doctrine MongoDB ODM to do the Document handling.<\/p>\n<p>The <code>GET \/homes\/{key}<\/code> and <code>POST \/homes<\/code> methods are working fine. The problem comes when I attempt to update an existing Home with <code>PUT \/homes\/{key}<\/code>.<\/p>\n<p>Here&#8217;s the current code:<\/p>\n<pre><code>\/**\n * PUT \/homes\/{key}\n *\n * Updates an existing Home.\n *\n * @param Request $request\n * @param string $key\n * @return Response\n * @throws HttpException\n *\/\npublic function putHomeAction(Request $request, $key)\n{\n    \/\/ check that the home exists\n    $home = $this-&gt;getRepository()-&gt;findOneBy(array('key' =&gt; (int) $key));\n\n    \/\/ disallow create via PUT as we want to generate key ourselves\n    if (!$home) {\n        throw new HttpException(403, 'Home key: '.$key.\" doesn't exist, to create use POST \/homes\");\n    }\n\n    \/\/ create object graph from JSON string\n    $updatedHome = $this-&gt;get('serializer')-&gt;deserialize(\n        $request-&gt;getContent(), 'Acme\\ApiBundle\\Document\\Home', 'json'\n    );\n\n    \/\/ replace existing Home with new data\n    $dm = $this-&gt;get('doctrine.odm.mongodb.document_manager');\n    $home = $dm-&gt;merge($updatedHome);\n    $dm-&gt;flush();\n\n    $view = View::create()\n        -&gt;setStatusCode(200)\n        -&gt;setData($home);\n\n    $response = $this-&gt;get('fos_rest.view_handler')-&gt;handle($view);\n    $response-&gt;setETag(md5($response-&gt;getContent()));\n    $response-&gt;setLastModified($home-&gt;getUpdated());\n\n    return $response;\n}\n<\/code><\/pre>\n<p>The JSON string passed to the action is successfully deserialized to my Document object graph by JMSSerializer, but when I attempt the merge &amp; flush, I get the error:<\/p>\n<pre><code>Notice: Undefined index:  in ....\/vendor\/doctrine\/mongodb-odm\/lib\/Doctrine\/ODM\/MongoDB\/Mapping\/ClassMetadataInfo.php line 1265\n<\/code><\/pre>\n<p>I&#8217;ve been attempting to follow the documentation here: http:\/\/docs.doctrine-project.org\/projects\/doctrine-mongodb-odm\/en\/latest\/reference\/working-with-objects.html#merging-documents<\/p>\n<p>Is there something I need to do to the deserialized Home before attempting to merge it? Is merge the wrong method?<\/p>\n<p>Thanks.<\/p>\n<ol>\n<li>\n<p>The only way I found to do this is to create a method in your document class that takes the updated document (e.g. <code>$updatedHome<\/code>) as a parameter and then just copies the required fields over into the existing document (e.g. <code>$home<\/code>).<\/p>\n<p>So above, the code:<\/p>\n<pre><code>\/\/ replace existing Home with new data\n$dm = $this-&gt;get('doctrine.odm.mongodb.document_manager');\n$home = $dm-&gt;merge($updatedHome);\n$dm-&gt;flush();\n<\/code><\/pre>\n<p>can be replaced with:<\/p>\n<pre><code>\/\/ replace existing Home with new data\n$home-&gt;copyFromSibling($updatedHome);\n$this-&gt;getDocumentManager()-&gt;flush();\n<\/code><\/pre>\n<p>and then it will work.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 12:01:52. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m writing a Symfony2 app that allows mobile users to create and update &#8220;Homes&#8221; via a REST service. I&#8217;m using MongoDB as the storage layer and Doctrine MongoDB ODM to do the Document handling. The GET \/homes\/{key} and POST \/homes methods are working fine. The problem comes when I attempt to update an existing Home [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1611","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1611"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1611\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}