[DDC-1643] PersistentCollection should support clone Created: 09/Feb/12  Updated: 17/Feb/12  Resolved: 17/Feb/12

Status: Resolved
Project: Doctrine 2 - ORM
Component/s: ORM
Affects Version/s: 2.2
Fix Version/s: 2.1.7, 2.2.1
Security Level: All

Type: Bug Priority: Major
Reporter: Bernhard Schussek Assignee: Benjamin Eberlei
Resolution: Fixed Votes: 0
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.



 Comments   
Comment by Benjamin Eberlei [ 09/Feb/12 ]

Moved to right project

Comment by Bernhard Schussek [ 10/Feb/12 ]

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

Comment by Benjamin Eberlei [ 10/Feb/12 ]

I guess its feasible, i have to think about:

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

Comment by Bernhard Schussek [ 10/Feb/12 ]

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

Comment by Josiah [ 13/Feb/12 ]

PR submitted for this issue: https://github.com/doctrine/doctrine2/pull/281

Comment by Benjamin Eberlei [ 17/Feb/12 ]

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,

Comment by Benjamin Eberlei [ 17/Feb/12 ]

Fixed in https://github.com/doctrine/doctrine2/commit/93f79d0810a158138020b4cca3332f9f0b1a76af and merged into 2.2 and 2.1.x

Generated at Thu Apr 24 17:11:35 UTC 2014 using JIRA 6.2.3#6260-sha1:63ef1d6dac3f4f4d7db4c1effd405ba38ccdc558.