Skip to content

Saving documents with embedded collections and default storage strategy leads to inconsistent state between in memory objects and database #2621

Open
@xammmue

Description

@xammmue

Bug Report

Q A
BC Break no
Version 2.7.0

Summary

I created a document structure that has two levels of embeddable document collections. Changing those collections by removing objects or setting them again leads to unexpected states being saved to the database. The state inside the database doesn't match the state of the object in memory anymore.
Changing the storage strategy to 'setArray' instead of using the default one resolves the issue

Current behavior

I setup the following example with the objects Bookshelf, Book and Pages.
A bookshelf has a collection of embedded books and a book has a collection of embedded pages.
I noticed unexpected behavior the following cases:

Removing one book from the collection of books and removing a page of another book.
In this case, the book gets removed as expected but the page that should have been removed stays.

Removing one book from the collection of books and resetting the pages of one of the remaining books.
In this case, the book gets removed as expected but the pages of an unrelated book get removed and the pages which were reset are duplicated.

How to reproduce

https://github.com/xammmue/doctrine_odm_save_storage_strategy_issue_poc

I created this repository which includes the classes Bookshelf, Book and Page. I also added some tests with assertions that would match the behavior I expected. The two failing tests test_removeBookAndPage and test_reinitializePagesAndDeleteOtherBook reproduce the (in my opinion) faulty behavior.

Changing the storage strategy for the books and pages to e.g. 'setArray' resolves the problems and all cases I observed work as expected.

Expected behavior

I expected that the state saved in the database matches the state of the objects in memory.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions