{"action":"closed","issue":{"url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1145","labels_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1145/labels{/name}","comments_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1145/comments","events_url":"https://api.github.com/repos/doctrine/mongodb-odm/issues/1145/events","html_url":"https://github.com/doctrine/mongodb-odm/issues/1145","id":88044406,"number":1145,"title":"@postUpdate events trigger at an unexpected moment when using EmbedMany","user":{"login":"blockjon","id":1198745,"avatar_url":"https://avatars.githubusercontent.com/u/1198745?v=3","gravatar_id":"","url":"https://api.github.com/users/blockjon","html_url":"https://github.com/blockjon","followers_url":"https://api.github.com/users/blockjon/followers","following_url":"https://api.github.com/users/blockjon/following{/other_user}","gists_url":"https://api.github.com/users/blockjon/gists{/gist_id}","starred_url":"https://api.github.com/users/blockjon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/blockjon/subscriptions","organizations_url":"https://api.github.com/users/blockjon/orgs","repos_url":"https://api.github.com/users/blockjon/repos","events_url":"https://api.github.com/users/blockjon/events{/privacy}","received_events_url":"https://api.github.com/users/blockjon/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/doctrine/mongodb-odm/labels/has%20PR","name":"has PR","color":"207de5"},{"url":"https://api.github.com/repos/doctrine/mongodb-odm/labels/idea","name":"idea","color":"fbca04"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/doctrine/mongodb-odm/milestones/1","html_url":"https://github.com/doctrine/mongodb-odm/milestones/1.0.0","labels_url":"https://api.github.com/repos/doctrine/mongodb-odm/milestones/1/labels","id":543770,"number":1,"title":"1.0.0","description":"","creator":{"login":"jmikola","id":244663,"avatar_url":"https://avatars.githubusercontent.com/u/244663?v=3","gravatar_id":"","url":"https://api.github.com/users/jmikola","html_url":"https://github.com/jmikola","followers_url":"https://api.github.com/users/jmikola/followers","following_url":"https://api.github.com/users/jmikola/following{/other_user}","gists_url":"https://api.github.com/users/jmikola/gists{/gist_id}","starred_url":"https://api.github.com/users/jmikola/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmikola/subscriptions","organizations_url":"https://api.github.com/users/jmikola/orgs","repos_url":"https://api.github.com/users/jmikola/repos","events_url":"https://api.github.com/users/jmikola/events{/privacy}","received_events_url":"https://api.github.com/users/jmikola/received_events","type":"User","site_admin":false},"open_issues":46,"closed_issues":43,"state":"open","created_at":"2014-01-22T04:49:23Z","updated_at":"2015-07-18T12:24:07Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2015-06-13T18:25:21Z","updated_at":"2015-07-18T12:24:07Z","closed_at":"2015-07-18T12:24:07Z","body":"My project uses strategy=set for nearly all of our EmbedMany elements and we also use @postUpdate hooks all over the place. \r\n\r\nGiven a document (such as book) with many embedded documents (such as chapters), if you make changes to both the \"surface\" of the document (such as the book title) and then you also modify the chapters at the same time and then call flush(), the sequence of queries and postUpdate callbacks seems wrong.\r\n\r\nWhen you flush() a change as I have described above, the sequence of events that takes place looks like this:\r\n\r\n1) MongoDB update query to update the surface of the db record (the book title).\r\n2) postUpdate hooks fired for *both* the surface and also for the embedded documents\r\n3) MongoDB update query to update the embedded documents\r\n\r\nImagine an app where you update the title of a book and modify a chapter in one flush() such that after the chapter is edited, you email alert the publisher via a @postUpdate hook on the Chapter document.\r\n\r\nBecause of the sequence of the events, the email is sent *before* the chapter is actually saved to the database server. This means that if there's a fatal MongoDB error on step 3, the email was still sent and the user is disappointed because the application sent the email and the data was never saved.\r\n\r\nThe whole idea of a \"postUpdate\" hook is that the the event would fire *after* the database record was saved....not before it was saved."}}