Exception thrown when using the same class of embedded document in two different parent documents #478

Closed
SteveTalbot opened this Issue Jan 22, 2013 · 2 comments

Comments

Projects
None yet
2 participants

Hi,

I may have discovered an obscure issue with Doctrine Mongo ODM, or this may just be me using the document manager incorrectly, but either way thought I'd post the issue here and my workaround for the benefit of the community.

Imagine you have three classes, A, B and X. Class X can be embedded into either A or B (note embedded, not referenced). If you follow this sequence of steps, you'll get an exception:

  1. Retrieve an document of class A and a document of class B from the database. In my case, A contained one instance of X with an embed-one relationship, and B contained one instance of X with an embed-many relationship.

$a = $dm->getRepository('A')->find($id_a);
$b = $dm->getRepository('B')->find($id_b);

  1. Remove the instance of X from B:

$b->children->remove($id_x);

  1. Save changes:

$dm->persist($b); // May not be necessary but doesn't seem to hurt
$dm->flush();

  1. The exception is thrown from line 892 of Doctrine\ODM\MongoDB\UnitOfWork.php: "A new document was found through a relationship that was not configured to cascade persist operations."

This arises because, at the point Doctrine tries to update $b in the database, there is still an instance of class X inside $a. I wouldn't expect this to prevent me from committing the change to $b to the database, but for some reason it does.

In my case, the workaround was simple: because I only needed to modify one object, I could detach the other from the document manager from attempting the flush.

However, it would be more convenient to be able to fetch both, modify either or both, then flush the change and let the document manager sort it out, without having to worry about detaching the unmodified object.

Kind regards,
Steve

malarzm added the has PR label Oct 27, 2015

Member

malarzm commented Oct 27, 2015

Most probably this will be fixed by #1252

malarzm added this to the 1.0.3 milestone Oct 28, 2015

Member

malarzm commented Oct 28, 2015

#1252 merged manually in de269fb

malarzm closed this Oct 28, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment