Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1925

Bug in UnitOfWork and ManyToMany relations

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.3
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None
    • Environment:
      symfony2

      Description

      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.

        Activity

        Andrew created issue -
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - What is the part you commented out? Also, in what context is your code executed?
        Hide
        Andrew added a comment -

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

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

        Show
        Andrew added a comment - I got error when $em->persist($forum); $em->flush(); executed. I can create github repository with code, which reproduce this error, if you want.
        Hide
        Marco Pivetta added a comment -

        Please do

        Show
        Marco Pivetta added a comment - Please do
        Hide
        Andrew added a comment -

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

        Show
        Andrew added a comment - that would be simple symfony2 application - will it work for you?
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - As long as the code is related to doctrine. Otherwise this issue is quite incomplete
        Hide
        Andrew added a comment -

        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

        Show
        Andrew added a comment - 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
        Hide
        Andrew added a comment -

        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

        Show
        Andrew added a comment - 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
        Hide
        Marco Pivetta added a comment -

        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.

        Show
        Marco Pivetta added a comment - 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.
        Marco Pivetta made changes -
        Field Original Value New Value
        Assignee Benjamin Eberlei [ beberlei ] Marco Pivetta [ ocramius ]
        Hide
        Andrew added a comment -

        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

        Show
        Andrew added a comment - 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
        Hide
        Marco Pivetta added a comment -

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

        Show
        Marco Pivetta added a comment - I think I spotted where this happens, but I don't have a clear overview on the situation. Will try to work on this...
        Hide
        Andrew added a comment -

        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())); }

        Show
        Andrew added a comment - 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())); }
        Hide
        Marco Pivetta added a comment -

        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.

        Show
        Marco Pivetta added a comment - 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.
        Hide
        Benjamin Eberlei added a comment -

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

        Show
        Benjamin Eberlei added a comment - A related Github Pull-Request [GH-402] was opened https://github.com/doctrine/doctrine2/pull/402
        Hide
        Benjamin Eberlei added a comment -

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

        Show
        Benjamin Eberlei added a comment - A related Github Pull-Request [GH-403] was opened https://github.com/doctrine/doctrine2/pull/403
        Hide
        Marco Pivetta added a comment -

        Duplicate of DDC-1210

        Show
        Marco Pivetta added a comment - Duplicate of DDC-1210
        Marco Pivetta made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.x [ 10090 ]
        Resolution Duplicate [ 3 ]
        Marco Pivetta made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Hide
        Benjamin Eberlei added a comment -

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

        Show
        Benjamin Eberlei added a comment - A related Github Pull-Request [GH-403] was closed https://github.com/doctrine/doctrine2/pull/403
        Hide
        Benjamin Eberlei added a comment -

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

        Show
        Benjamin Eberlei added a comment - A related Github Pull-Request [GH-402] was closed https://github.com/doctrine/doctrine2/pull/402
        Benjamin Eberlei made changes -
        Resolution Duplicate [ 3 ]
        Status Closed [ 6 ] Reopened [ 4 ]
        Benjamin Eberlei made changes -
        Fix Version/s 2.3 [ 10185 ]
        Fix Version/s 2.1.7 [ 10198 ]
        Fix Version/s 2.x [ 10090 ]
        Benjamin Eberlei made changes -
        Fix Version/s 2.1.8 [ 10220 ]
        Fix Version/s 2.1.7 [ 10198 ]
        Benjamin Eberlei made changes -
        Status Reopened [ 4 ] Resolved [ 5 ]
        Resolution Fixed [ 1 ]
        Hide
        Doctrine Bot added a comment -

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

        Show
        Doctrine Bot added a comment - A related Github Pull-Request [GH-402] was closed: https://github.com/doctrine/dbal/pull/402
        Hide
        Doctrine Bot added a comment -

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

        Show
        Doctrine Bot added a comment - A related Github Pull-Request [GH-403] was closed: https://github.com/doctrine/dbal/pull/403
        Guilherme Blanco made changes -
        Fix Version/s 2.1.8 [ 10220 ]

        This list may be incomplete, as errors occurred whilst retrieving source from linked applications:

        • Request to http://www.doctrine-project.org/fisheye/ failed: Error in remote call to 'FishEye 0 (http://www.doctrine-project.org/fisheye/)' (http://www.doctrine-project.org/fisheye) [AbstractRestCommand{path='/rest-service-fe/search-v1/crossRepositoryQuery', params={query=DDC-1925, expand=changesets[0:20].revisions[0:29],reviews}, methodType=GET}] : Received status code 503 (Service Temporarily Unavailable)

          People

          • Assignee:
            Marco Pivetta
            Reporter:
            Andrew
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: