doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 504 forks source link

Can't flush only specific documents #1149

Closed dorongutman closed 6 years ago

dorongutman commented 9 years ago

I'm trying to call flush([$documentOne, $documentThree]) so it'll only flush these two documents and not others which I have not specified.

However, the ODM flushes all the documents that are pending. Specifically, it flushes a scheduled "insert" which is a new document that's not referenced by any other document.

malarzm commented 9 years ago

After some internal discussion it turned out that flush() with documents specified (or entities in ORM's case, where behaviour is the same) was not really meant to be "we will save just these and nothing else" restriction. This is documented here but I think it could be exposed better than on UoW's private method :)

dorongutman commented 9 years ago

@malarzm oh wow, that's very much confusing.. http://doctrine-mongodb-odm.readthedocs.org/en/latest/reference/working-with-objects.html#flushing-single-documents

Anyways - is there any way of flushing only a single document ? Maybe with the querybuilder or something ?

malarzm commented 9 years ago

@dorongutman heh yes, you're right, we need to write a disclaimer there as well... To be honest I was a bit surprised as well, but this predates my work here very much ;) Anyway right now I see two possible ways:

  1. As you suggest QueryBuilder might be the way to go (but there will be no events and preparation)
  2. Change your flow to not schedule unwanted documents or detach them first

Also there's a chance we may diverge from ORM here and actually allow flushing only specified documents. One of main obstacles would be that current commit saves all collections BUT #1155 changes that to update collections only when their owner is updated (so outstanding things will be orphan removal and updating UoW internal state). I've marked this yesterday as an idea to allow discussion whether we want to introduce the change or not

alcaeus commented 6 years ago

1707 tracks the removal of flush($document) in 2.0 - the correct solution is to use change tracking policies.