Uploaded image for project: 'Doctrine 2 - ORM'
  1. Doctrine 2 - ORM
  2. DDC-875

Merge can sometimes add the same entity twice into a collection

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Cannot Reproduce
    • Affects Version/s: 2.0-BETA4
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      When merging some cascade merge object-graphs, the same entity in a ManyToMany association can be put into a collection twice during doMerge.

      The attached patch should stop this from happening.

      1. multipleaddmerge.diff
        1 kB
        Dave Keen
      2. multipleaddmerge2.diff
        1 kB
        Dave Keen

        Activity

        Hide
        viveksoni.net Vivek Soni added a comment -

        This stackoverflow question solved my problem, instead of persist, i had to do merge

        http://stackoverflow.com/questions/18215975/doctrine-a-new-entity-was-found-through-the-relationship

        Show
        viveksoni.net Vivek Soni added a comment - This stackoverflow question solved my problem, instead of persist, i had to do merge http://stackoverflow.com/questions/18215975/doctrine-a-new-entity-was-found-through-the-relationship
        Hide
        viveksoni.net Vivek Soni added a comment -

        I am also facing this issue, I have String as primary key for a table (brand table) getting this error

        A new entity was found through the relationship 'Application\Entity\Pcds#brandCode' that was not configured to cascade persist operations for entity: Company\Model\Entity\Brand@000000003beb6d2200007ffa52ab9a34. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade=

        {"persist"}

        ). If you cannot find out which entity causes the problem implement 'Company\Model\Entity\Brand#__toString()' to get a clue.

        If i var_dump this $brand entity it shows that it have primary key inside it.

        Show
        viveksoni.net Vivek Soni added a comment - I am also facing this issue, I have String as primary key for a table (brand table) getting this error A new entity was found through the relationship 'Application\Entity\Pcds#brandCode' that was not configured to cascade persist operations for entity: Company\Model\Entity\Brand@000000003beb6d2200007ffa52ab9a34. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade= {"persist"} ). If you cannot find out which entity causes the problem implement 'Company\Model\Entity\Brand#__toString()' to get a clue. If i var_dump this $brand entity it shows that it have primary key inside it.
        Hide
        asm89 Alexander added a comment -

        We cannot reproduce this error and haven't had similar complaints ever-since. Feel free to open a new issue with a failing testcase.

        Show
        asm89 Alexander added a comment - We cannot reproduce this error and haven't had similar complaints ever-since. Feel free to open a new issue with a failing testcase.
        Hide
        beberlei Benjamin Eberlei added a comment -

        It seems even this issue is caused by multiple calls to persist. I cannot reproduce this with just a single bidirectional cascade merge.

        Show
        beberlei Benjamin Eberlei added a comment - It seems even this issue is caused by multiple calls to persist. I cannot reproduce this with just a single bidirectional cascade merge.
        Hide
        ccapndave Dave Keen added a comment -

        Sorry, I am still getting the hang of git and diff and maybe what I put in there isn't what I meant to. I have now pushed the code to the DDC-875 branch on my ccapndave/doctrine2 fork on GitHub, hopefully this works better.

        As far as I can tell I am using unwrap() in order to check whether the element already exists in the array, but then calling >add() directly on the PersistentCollection rather than the ArrayCollection, triggering $this>changed().

        Show
        ccapndave Dave Keen added a comment - Sorry, I am still getting the hang of git and diff and maybe what I put in there isn't what I meant to. I have now pushed the code to the DDC-875 branch on my ccapndave/doctrine2 fork on GitHub, hopefully this works better. As far as I can tell I am using unwrap() in order to check whether the element already exists in the array, but then calling >add() directly on the PersistentCollection rather than the ArrayCollection, triggering $this >changed().
        Hide
        beberlei Benjamin Eberlei added a comment - - edited

        That is exactly the same code in the patch, the lines are just formatted differently.

        Show
        beberlei Benjamin Eberlei added a comment - - edited That is exactly the same code in the patch, the lines are just formatted differently.
        Hide
        ccapndave Dave Keen added a comment -

        Oops - that was a mistake. I have attached multipleaddmerge2.diff which no longer uses unwrap to add the element.

        Show
        ccapndave Dave Keen added a comment - Oops - that was a mistake. I have attached multipleaddmerge2.diff which no longer uses unwrap to add the element.
        Hide
        beberlei Benjamin Eberlei added a comment -

        not doint the unwrap() but add() directly was a bugfix for one of your other issues .Why is unwrap in here again?

        Show
        beberlei Benjamin Eberlei added a comment - not doint the unwrap() but add() directly was a bugfix for one of your other issues .Why is unwrap in here again?
        Hide
        beberlei Benjamin Eberlei added a comment -

        which conditions lead to this problem? I want to write a test for it

        Show
        beberlei Benjamin Eberlei added a comment - which conditions lead to this problem? I want to write a test for it

          People

          • Assignee:
            romanb Roman S. Borschel
            Reporter:
            ccapndave Dave Keen
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: