Quantcast
Channel: Symfony2 and Doctrine: how to fetch two different object for the same id? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Symfony2 and Doctrine: how to fetch two different object for the same id?

$
0
0

I have this scenario:

  • Object A have some reference to other objects B,C,D
  • Object B have some reference to other objects A,F,G
  • Object C have some reference to other objects A,...

And so on.

In my code, I need to make a "copy" of an object (say A) for tmp reasons (no, I can't use a different structure, I need to have a copy of object).

If I use clone, obviously, I make a clone of my object but object related to him, aren't cloned.
I perfeclty know that I can override magic-method __clone() in order to assign to - from A object point of view - B,C,D as clone of objects themselves, but I have so many objects (and many of them are contained into ArrayCollection for Doctrine purpose) and I would prefer to avoid the override of each object's clone function.

Alternatively, I thought that I can refetch an object from doctrine to make a new one, in that way:

$aCopy = $this->entity_manager                       ->getRepository('MyBundle:A')                       ->find($a->getId());

where $a is an instance of class A

After doing this operation - that of course is "wrong" because I suspect that doctrine will mark that object as "alredy fetched" and return its pointer()* - I simply print the ID of my two objects with spl_object_hash() function and, of course again, they refer to the same object ID, so to the same object.

PS.:

I can't use doctrine detach() function because i need to have the original object available after this operation

Question

How can I tackle this situation? As you can see, I've tryied two different ways and no one of them had satisfied me.

Note

I've tagged php also, because if someone could point me to a different solution, php-pure based, I'll take it into account also

(*)

In this case the Article is accessed from the entity manager twice,but modified in between. Doctrine 2 realizes this and will only evergive you access to one instance of the Article with ID 1234, no matterhow often do you retrieve it from the EntityManager and even no matterwhat kind of Query method you are using (find, Repository Finder orDQL). This is called “Identity Map” pattern, which means Doctrinekeeps a map of each entity and ids that have been retrieved per PHPrequest and keeps returning you the same instances.

That confirm what I've said previously


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images