Symfony2和Doctrine:如何为相同的id获取两个不同的对象? [英] Symfony2 and Doctrine: how to fetch two different object for the same id?

查看:84
本文介绍了Symfony2和Doctrine:如何为相同的id获取两个不同的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种情况:


  • 对象A有一些参考其他对象B,C,D
  • $ b $对象B有一些对其他对象的引用A,F,G
  • 对象C有一些引用其他对象A,...

  • 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,...

等等。

在我的代码中,我需要做一个对象的副本 A)对于tmp原因(不,我不能使用不同的结构,我需要一个对象的副本)。

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).

如果我使用克隆,显然,我使我的对象的克隆,但与他相关的对象,不被克隆。

我完美地知道我可以覆盖魔法方法 __ clone()为了分配到 - 从A对象的角度来看,B,C,D作为对象本身的克隆,但是我有这么多对象(其中很多是包含到 ArrayCollection for Doctrine目的),我宁愿避免覆盖每个对象的克隆功能。

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());

其中 $ a 是一个 class A

执行此操作后,当然是错误的,因为我怀疑教义会将该对象标记为alredy fetched并返回其指针()* - 我只需使用 spl_object_hash()函数打印我的两个对象的ID,再次,他们引用相同的对象ID,所以对同一个对象。

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.

我不能使用doctrine detach()函数,因为我需要在此操作后可以使用原始对象

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

如何处理这种情况?你可以看到,我已经尝试了两种不同的方式,没有人满足了我。

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

我也标记了PHP,因为如果有人可以指出一个不同的解决方案,php-pure,我也会考虑到

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

(* )


在这种情况下,文章从实体经理两次访问,
,但之间进行了修改。 Doctrine 2意识到这一点,只有
可以让你访问一个ID为1234的文章的一个实例,不管
你多久从EntityManager中获取它,甚至不管
什么样的的Query方法(查找,Repository Finder或
DQL)。这被称为身份映射模式,这意味着Doctrine
保留每个实体的映射以及根据PHP
请求检索的id,并继续返回相同的实例。

In this case the Article is accessed from the entity manager twice, but modified in between. Doctrine 2 realizes this and will only ever give you access to one instance of the Article with ID 1234, no matter how often do you retrieve it from the EntityManager and even no matter what kind of Query method you are using (find, Repository Finder or DQL). This is called "Identity Map" pattern, which means Doctrine keeps a map of each entity and ids that have been retrieved per PHP request and keeps returning you the same instances.

确认我以前说过的

推荐答案

不如我预期的那样复杂。

Answer was less complex than I expected.

似乎是足够的调用 $ this-> entity_manager-> clear(); 将清除此实体映射并强制将其从数据库重新加载到全新的对象!

It seem to be sufficent call $this->entity_manager->clear(); that will clear this entity map and force it to reload from database into a brand new object!

$this->entity_manager->clear();
$aCopy = $this->entity_manager
                       ->getRepository('MyBundle:A')
                       ->find($a->getId());
            $this->logger->debug('Original Obj: '.spl_object_hash($a));
            $this->logger->debug('Copied Obj:      '.spl_object_hash($aCopy));

这将打印


[2013-02-08 12:07:20] app.DEBUG:原始对象:
000000006523 645c 000000004b1160d1 [] [] [2013-02-08 12:07 :20]
app.DEBUG:Copied Obj:000000006523 66e3 000000004b1160d1 [] []

[2013-02-08 12:07:20] app.DEBUG: Original Obj: 000000006523645c000000004b1160d1 [] [] [2013-02-08 12:07:20] app.DEBUG: Copied Obj: 00000000652366e3000000004b1160d1 [] []

这篇关于Symfony2和Doctrine:如何为相同的id获取两个不同的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆