I have a scenario like this:
- Object A has some reference to other objects B, C, D
- Object B has some reference to other objects A, F, G
- Object C has some reference to other objects A, ...
Etc.
In my code I need to make a "copy" of the object (for example, A) for tmp reasons (no, I cannot use another structure, I need to have a copy of the object).
If I use clone , obviously I am creating a clone of my object, but the object associated with it is not cloned.
I know very well that I can redefine magic-method __clone() to assign - from the object point of view - B, C, D as cloning the objects themselves, but I have so many objects (and many of them are contained in ArrayCollection for the purpose of Doctrine) , and I would rather avoid overriding each cloning function of the object.
Alternatively, I thought I could restore an object from a doctrine to create a new one:
$aCopy = $this->entity_manager ->getRepository('MyBundle:A') ->find($a->getId());
where $a is an instance of class A
After performing this operation, this is of course “wrong” because I suspect that the doctrine marks this object as “alredy fetched” and returns its pointer () * - I just print the identifier of my two objects using the spl_object_hash() function and Of course, again, they refer to the same object identifier, therefore, to the same object.
PS :.
I cannot use the doctrine detach() function because I need to have the source object accessible after this operation
Question
How can I solve this situation? As you can see, I tried two different ways, and none of them satisfied me.
Note
I also noted php, because if someone can point me to another solution based on php-pure, I will take it into account as well
(*)
In this case, the article gets access from the object manager twice, but changes between them. Doctrine 2 is aware of this and will only ever give you access to one copy of the article with the identifier 1234, no matter how often you extract it from the EntityManager and it doesn’t matter which query method you use (find, find repository or DQL). This is called the "Identity Map" template, which means "Doctrine" stores a map of each object and identifiers that were received on the PHP request and returns the same instances to you.
Confirm what I said earlier