DDC-1925: Bug in UnitOfWork and ManyToMany relations #2594

Closed
doctrinebot opened this Issue Jul 14, 2012 · 20 comments

2 participants

@doctrinebot

Jira issue originally created by user zhil:

Lets say, I have entity Forum with ManyToMany relations with User.
I need to validate user changes and I use code like

$uow = $this->getDoctrine()->getEntityManager()->getUnitOfWork();
$uow->computeChangeSets();
$changeSet = $uow->getEntityChangeSet($forum);
if (.... bla-bla-bla....) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($forum);
$em->flush();
}

Unfortunately, whenever I try to change manyToMany relations - I got error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '4-4' for key 'PRIMARY'

If I comment uow code - everything works just great.

It looks like bug in UnitOfWork implementation.

Let me know if you need more details from me.

@doctrinebot

Comment created by @ocramius:

What is the part you commented out? Also, in what context is your code executed?

@doctrinebot

Comment created by zhil:

I got error when $em->persist($forum); $em->flush(); executed.

I can create github repository with code, which reproduce this error, if you want.

@doctrinebot

Comment created by @ocramius:

Please do

@doctrinebot

Comment created by zhil:

that would be simple symfony2 application - will it work for you?

@doctrinebot

Comment created by @ocramius:

As long as the code is related to doctrine. Otherwise this issue is quite incomplete :)

@doctrinebot

Comment created by zhil:

Done, please check https://github.com/zhil/testDoctrine

In few words, when I add code like
$uow = $this->getDoctrine()->getEntityManager()->getUnitOfWork();
$uow->computeChangeSets();
$changeSet = $uow->getEntityChangeSet($product);

before $em->persist(); $em->flush();

I got fake MYSQL error :)

Thank in advance for your help :)

@doctrinebot

Comment created by zhil:

Just wonder - is it bug in doctrine2 or its problems somewhere else? (symfony2/my code/ etc.)

This is part of the live project - I need to fix it :)

@doctrinebot

Comment created by @ocramius:

I think it is related with the fact that you're using the UnitOfWork manually. You can probably try to fix the problem by moving your code to an event subscriber until this is fixed.

@doctrinebot

Comment created by zhil:

Well, I used UnitOfWork, because I need to know what was changed during object validation (for example, property status can be changed only in predefined cases etc.).

Ok, thanks for the suggestion - I will implement some temporary solution for this problem :)

@doctrinebot

Comment created by @ocramius:

I think I spotted where this happens, but I don't have a clear overview on the situation. Will try to work on this...

@doctrinebot

Comment created by zhil:

Thanks for the checking this issue.
I have already patched my application with ugly patch. Just in case solution will take some time and somebody else will need similar patch. I patched entity like

entity {
public $previousStatusBugfix = -1;
public function setStatus($status)
{
// check http://www.doctrine-project.org/jira/browse/[DDC-1925](http://www.doctrine-project.org/jira/browse/DDC-1925)?focusedCommentId=18344#comment-18344
// Ticket #651
$this->previousStatusBugfix = $this->status;
$this->status = $status;
}
}

and validator

if(($object->previousStatusBugfix != -1) && ($object->previousStatusBugfix != $object->getStatus())) {
$changeSet = array("status"=>array(0=>$object->previousStatusBugfix, 1=>$object->getStatus()));
}

:)

@doctrinebot

Comment created by @ocramius:

I just wrote a couple of tests (attaching them to the issue shortly) and found out that on >=2.2.x your code runs perfectly.
The problem is on the 2.1.x branch, and the commit that fixed the issue is 4474d30 for DDC-1210

Now looking if it can be merged into 2.1.x since it doesn't seem to cause any BC break.

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-402] was opened
#402

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-403] was opened
#403

@doctrinebot

Comment created by @ocramius:

Duplicate of DDC-1210

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-403] was closed
#403

@doctrinebot

Comment created by @beberlei:

A related Github Pull-Request [GH-402] was closed
#402

@doctrinebot

Issue was closed with resolution "Fixed"

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-402] was closed:
doctrine/dbal#402

@doctrinebot

Comment created by @doctrinebot:

A related Github Pull-Request [GH-403] was closed:
doctrine/dbal#403

@Ocramius Ocramius was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.3 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment