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

Closed
opened 2026-01-22 13:52:47 +01:00 by admin · 20 comments
Owner

Originally created by @doctrinebot on GitHub (Jul 14, 2012).

Originally assigned to: @Ocramius on GitHub.

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.

Originally created by @doctrinebot on GitHub (Jul 14, 2012). Originally assigned to: @Ocramius on GitHub. 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.
admin added the Bug label 2026-01-22 13:52:47 +01:00
admin closed this issue 2026-01-22 13:52:47 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

Comment created by @ocramius:

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

@doctrinebot commented on GitHub (Jul 16, 2012): Comment created by @ocramius: What is the part you commented out? Also, in what context is your code executed?
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

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 commented on GitHub (Jul 16, 2012): 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.
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

Comment created by @ocramius:

Please do

@doctrinebot commented on GitHub (Jul 16, 2012): Comment created by @ocramius: Please do
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

Comment created by zhil:

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

@doctrinebot commented on GitHub (Jul 16, 2012): Comment created by zhil: that would be simple symfony2 application - will it work for you?
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

Comment created by @ocramius:

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

@doctrinebot commented on GitHub (Jul 16, 2012): Comment created by @ocramius: As long as the code is related to doctrine. Otherwise this issue is quite incomplete :)
Author
Owner

@doctrinebot commented on GitHub (Jul 16, 2012):

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 commented on GitHub (Jul 16, 2012): 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 :)
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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 commented on GitHub (Jul 19, 2012): 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 :)
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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 commented on GitHub (Jul 19, 2012): 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.
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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 commented on GitHub (Jul 19, 2012): 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 :)
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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 commented on GitHub (Jul 19, 2012): 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...
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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?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 commented on GitHub (Jul 19, 2012): 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())); } :)
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

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 https://github.com/doctrine/doctrine2/commit/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 commented on GitHub (Jul 19, 2012): 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 https://github.com/doctrine/doctrine2/commit/4474d30 for [DDC-1210](http://www.doctrine-project.org/jira/browse/DDC-1210) Now looking if it can be merged into `2.1.x` since it doesn't seem to cause any BC break.
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

Comment created by @beberlei:

A related Github Pull-Request [GH-402] was opened
https://github.com/doctrine/doctrine2/pull/402

@doctrinebot commented on GitHub (Jul 19, 2012): Comment created by @beberlei: A related Github Pull-Request [GH-402] was opened https://github.com/doctrine/doctrine2/pull/402
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

Comment created by @beberlei:

A related Github Pull-Request [GH-403] was opened
https://github.com/doctrine/doctrine2/pull/403

@doctrinebot commented on GitHub (Jul 19, 2012): Comment created by @beberlei: A related Github Pull-Request [GH-403] was opened https://github.com/doctrine/doctrine2/pull/403
Author
Owner

@doctrinebot commented on GitHub (Jul 19, 2012):

Comment created by @ocramius:

Duplicate of DDC-1210

@doctrinebot commented on GitHub (Jul 19, 2012): Comment created by @ocramius: Duplicate of [DDC-1210](http://www.doctrine-project.org/jira/browse/DDC-1210)
Author
Owner

@doctrinebot commented on GitHub (Jul 23, 2012):

Comment created by @beberlei:

A related Github Pull-Request [GH-403] was closed
https://github.com/doctrine/doctrine2/pull/403

@doctrinebot commented on GitHub (Jul 23, 2012): Comment created by @beberlei: A related Github Pull-Request [GH-403] was closed https://github.com/doctrine/doctrine2/pull/403
Author
Owner

@doctrinebot commented on GitHub (Jul 29, 2012):

Comment created by @beberlei:

A related Github Pull-Request [GH-402] was closed
https://github.com/doctrine/doctrine2/pull/402

@doctrinebot commented on GitHub (Jul 29, 2012): Comment created by @beberlei: A related Github Pull-Request [GH-402] was closed https://github.com/doctrine/doctrine2/pull/402
Author
Owner

@doctrinebot commented on GitHub (Jul 29, 2012):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jul 29, 2012): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Nov 12, 2013):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-402] was closed:
https://github.com/doctrine/dbal/pull/402

@doctrinebot commented on GitHub (Nov 12, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-402] was closed: https://github.com/doctrine/dbal/pull/402
Author
Owner

@doctrinebot commented on GitHub (Nov 13, 2013):

Comment created by @doctrinebot:

A related Github Pull-Request [GH-403] was closed:
https://github.com/doctrine/dbal/pull/403

@doctrinebot commented on GitHub (Nov 13, 2013): Comment created by @doctrinebot: A related Github Pull-Request [GH-403] was closed: https://github.com/doctrine/dbal/pull/403
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2430