{"action":"created","issue":{"url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1058","labels_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1058/labels{/name}","comments_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1058/comments","events_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1058/events","html_url":"https://github.com/doctrine/mongodb-odm/issues/1058","id":64657418,"number":1058,"title":"Document is upserted and then updated during one flush operation","user":{"login":"vkartaviy","id":648039,"avatar_url":"https://avatars.githubusercontent.com/u/648039?v=3","gravatar_id":"","url":"https://api.github.com/users/vkartaviy","html_url":"https://github.com/vkartaviy","followers_url":"https://api.github.com/users/vkartaviy/followers","following_url":"https://api.github.com/users/vkartaviy/following{/other_user}","gists_url":"https://api.github.com/users/vkartaviy/gists{/gist_id}","starred_url":"https://api.github.com/users/vkartaviy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkartaviy/subscriptions","organizations_url":"https://api.github.com/users/vkartaviy/orgs","repos_url":"https://api.github.com/users/vkartaviy/repos","events_url":"https://api.github.com/users/vkartaviy/events{/privacy}","received_events_url":"https://api.github.com/users/vkartaviy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-26T23:42:35Z","updated_at":"2015-03-27T14:30:30Z","closed_at":null,"body":"* I have document with ```pre-generated id``` so it added to ```documentUpserts``` array after calling ```$dm->persist($document)```\r\n\r\n* I have ```onFlush``` listener where I add some data to that document and then call ```$dm->getUnitOfWork()->recomputeSingleDocumentChangeSet($metadata, $document);```\r\n\r\n* I call ```$dm->flush()``` and see the next picture:\r\n```documentUpserts``` and ```documentUpdates``` has the same document to update.\r\n\r\nIt looks like the problem is [here](https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/UnitOfWork.php#L814),  in direct adding of the document to the ```documentUpdates``` array but should be through ```scheduleForUpdate``` method?\r\n"},"comment":{"url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/comments/86958370","html_url":"https://github.com/doctrine/mongodb-odm/issues/1058#issuecomment-86958370","issue_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1058","id":86958370,"user":{"login":"vkartaviy","id":648039,"avatar_url":"https://avatars.githubusercontent.com/u/648039?v=3","gravatar_id":"","url":"https://api.github.com/users/vkartaviy","html_url":"https://github.com/vkartaviy","followers_url":"https://api.github.com/users/vkartaviy/followers","following_url":"https://api.github.com/users/vkartaviy/following{/other_user}","gists_url":"https://api.github.com/users/vkartaviy/gists{/gist_id}","starred_url":"https://api.github.com/users/vkartaviy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkartaviy/subscriptions","organizations_url":"https://api.github.com/users/vkartaviy/orgs","repos_url":"https://api.github.com/users/vkartaviy/repos","events_url":"https://api.github.com/users/vkartaviy/events{/privacy}","received_events_url":"https://api.github.com/users/vkartaviy/received_events","type":"User","site_admin":false},"created_at":"2015-03-27T14:30:30Z","updated_at":"2015-03-27T14:30:30Z","body":"Yep, it looks like the same issue but I would suggest to fix it with below patch, I tried it in my project and it goes well...\r\n\r\n```diff\r\ndiff --git a/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php b/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php\r\nindex 6a34e25..562cbe0 100644\r\n--- a/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php\r\n+++ b/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php\r\n@@ -811,7 +811,8 @@ class UnitOfWork implements PropertyChangedListener\r\n                     : $changeSet;\r\n\r\n                 $this->originalDocumentData[$oid] = $actualData;\r\n-                $this->documentUpdates[$oid] = $document;\r\n+                $this->scheduleForUpdate($document);\r\n+                //$this->documentUpdates[$oid] = $document;\r\n             }\r\n         }\r\n\r\n@@ -840,7 +841,8 @@ class UnitOfWork implements PropertyChangedListener\r\n                         if (isset($this->documentChangeSets[$oid2])) {\r\n                             $this->documentChangeSets[$oid][$mapping['fieldName']] = array($value, $value);\r\n                             if ( ! $isNewDocument) {\r\n-                                $this->documentUpdates[$oid] = $document;\r\n+                                $this->scheduleForUpdate($document);\r\n+                                //$this->documentUpdates[$oid] = $document;\r\n                             }\r\n                             break;\r\n                         }\r\n```"}}