doctrine / DoctrineMongoDBBundle

Integrates Doctrine MongoDB ODM with Symfony
http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html
MIT License
382 stars 230 forks source link

Embedded document issue #440

Closed redayoub closed 6 years ago

redayoub commented 6 years ago

I've noticed a weird issue related to embedded documents. I've two collections User and City:

# User.php

/**
 * @ODM\EmbedOne(targetDocument="AppBundle\Document\City")
 */
protected $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.

alcaeus commented 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:

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.

alcaeus commented 6 years ago

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.

redayoub commented 6 years ago

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.

alcaeus commented 6 years ago

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.

redayoub commented 6 years ago

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.

alcaeus commented 6 years ago

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.

redayoub commented 6 years ago

I didn't know about that, thanks a lot for your help.