How to synchronize a Doctrine 2 collection?
With Doctrine 2, if you have (for example) a tournament with 2 sponsors (ids 1 and 2), and a user then removes sponsor 1 and adds sponsor 3 in one action (like from a list), what is the best way to sync the changes to the database, so that there is only one delete query and one insert query?
The UI w开发者_如何学Goill provide me with the new set of ids, for example "1,3", and I need to use that to determine what has been added, and what has been removed.
I had the same situation for linking books to authors. If I wanted to update the author list (via pivot aka linking table) I would just remove all author ids from the pivot table where book_id = 1, then add the new list of authors. This way you don't have to introduce complex logic to determine diffs. Also, it's best if this is wrapped in a transaction.
精彩评论