Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-1643

PersistentCollection should support clone

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.2
    • Fix Version/s: 2.1.7, 2.2.1
    • Component/s: ORM
    • Security Level: All
    • Labels:
      None

      Description

      I'm trying to clone a PersistentCollection and expect to get a stable snapshot. But the class does not implement __clone, thus both objects refer to the same internal ArrayCollection.

      I can't use takeSnapshot because I'm writing generic code that should work with any implementation of Traversable/ArrayAccess.

        Activity

        Hide
        Benjamin Eberlei added a comment -

        Moved to right project

        Show
        Benjamin Eberlei added a comment - Moved to right project
        Hide
        Bernhard Schussek added a comment -

        Thank you for moving. Any comments on the feasibility of this issue?

        Show
        Bernhard Schussek added a comment - Thank you for moving. Any comments on the feasibility of this issue?
        Hide
        Benjamin Eberlei added a comment -

        I guess its feasible, i have to think about:

        1. how to fix the issue
        2. what behavior to attach in general.

        Show
        Benjamin Eberlei added a comment - I guess its feasible, i have to think about: 1. how to fix the issue 2. what behavior to attach in general.
        Hide
        Bernhard Schussek added a comment -

        Ok. For the sake of completeness, this issue directly causes another in Symfony2 which was already partially fixed here: https://github.com/symfony/symfony/pull/3315

        Show
        Bernhard Schussek added a comment - Ok. For the sake of completeness, this issue directly causes another in Symfony2 which was already partially fixed here: https://github.com/symfony/symfony/pull/3315
        Hide
        Josiah added a comment -
        Show
        Josiah added a comment - PR submitted for this issue: https://github.com/doctrine/doctrine2/pull/281
        Hide
        Benjamin Eberlei added a comment -

        What are you doing after the PersistentCollection is cloned?

        The reason is, that if the code looks like this:

        $col = clone $entity->getCol();
        $otherEntity->setCol($col);
        echo count($col);
        $em->flush();
        

        It will do very weird stuff using the owner of the association $col which is $entity. It will also try to insert the $col for $entity not for $otherEntity. To prevent this we have to do some work,

        Show
        Benjamin Eberlei added a comment - What are you doing after the PersistentCollection is cloned? The reason is, that if the code looks like this: $col = clone $entity->getCol(); $otherEntity->setCol($col); echo count($col); $em->flush(); It will do very weird stuff using the owner of the association $col which is $entity. It will also try to insert the $col for $entity not for $otherEntity. To prevent this we have to do some work,
        Hide
        Benjamin Eberlei added a comment -
        Show
        Benjamin Eberlei added a comment - Fixed in https://github.com/doctrine/doctrine2/commit/93f79d0810a158138020b4cca3332f9f0b1a76af and merged into 2.2 and 2.1.x

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Bernhard Schussek
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: