Closed redayoub closed 6 years ago
Everything works fine when I user ReferenceOne instead of EmbedOne.
That's because what you're doing is referencing another document.
Embedded documents are different from references in a few key areas:
orphanRemoval
). Normally, this is not a problem because it's just a remove
in UnitOfWork, except that when you embed a Document
instead of an EmbeddedDocument
, it will actually remove the original document from the database.After that if I go to the user's collection edit form and try change the city this give me a very weird behavior
I suppose you're doing what amounts to $user->setCity($otherCity)
. When computing changesets for the user, it sees that there's a different city in the user object, persists this new city and removes the old one (triggering removal in the city collection since it's not an embedded document).
All in all, I believe you're actually thinking of a reference, not an embedded relationship.
Last but not least, this is not an issue in the bundle. Please raise an issue at http://github.com/doctrine/mongodb-odm/issues if you believe you have found a bug. Closing here.
Thanks for your explanation. I think I'll open an issue there because it seems illogical for me removing a document from its collection if we edit or set to null its embedded reference in an other collection.
I'll save you the trouble of writing that in a new issue: this is invalid behavior that isn't supported. Use an EmbeddedDocument
. If you want to reference an object in a different collection, use RefererenceOne
instead of EmbedOne
.
I just tested with EmbeddedDocument and I've the same behaviour. I'm using EmbedOne instead of ReferenceoOne in order to reduce the number of queries because if I get the list of 50 users for example and I try to show the city for each user it generates 50 additional queries.
Not sure how to best say this, but you're using a hammer to tighten a loose screw. Your problem can be solved by priming references: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/priming-references.html.
I didn't know about that, thanks a lot for your help.
I've noticed a weird issue related to embedded documents. I've two collections User and City:
When I create a new User document, I select a city from the a select box and everything is ok, the city selected is embedded inside the user document. After that if I go to the user's collection edit form and try change the city this give me a very weird behavior, instead of editing the city document embedded inside the user document, the city remains unchanged and I got the city removed from the City collection.
Everything works fine when I user ReferenceOne instead of EmbedOne.