获得“真实”在doctrine2中的一个代理对象的对象

问题描述:

Doctrine使用代理对象来表示相关对象,以便于延迟加载。这是一个非常酷的功能,但它导致了我正在尝试完成的一个问题。

Doctrine uses proxy objects to represent related objects in order to facilitate lazy loading. This is a really cool feature, but its causing an issue with something I am trying to accomplish.

我已经定制了我的用户对象,以便他们都需要相关到一个不同的对象,我会称之为城市。这种关系正常工作。

I have customized my user object so that they are all required to be related to a different object, which I will call city. This relationship is working fine.

我有一个表单,我的用户填写以生成另一个对象,街道。街道也与城市对象有关。而不是让我的用户在填写表单时选择城市,我想在我将对象保存到我的数据库之前自动设置。

I have a form that my user fills out to generate another object, street. Street is also related to the city object. Instead of having my user select the city when they fill out the form, I want to automatically set it before I persist the object to my database.

我尝试使用 $ event-> setCity($ user-> getCity()),但是由于$ user-> getCity()返回一个代理对象,所以会产生错误。有没有可以从代理对象调用的函数来获取真实的代码?

I tried using $event->setCity($user->getCity()), but since $user->getCity() returns a proxy object, this generates an error. Is there a function I can call from the proxy object to get the real one?

注意:我知道我可以使用一个连接来创建一个自定义查询强制原则实际加载相关对象,但是由于这是用户(使用FOSUserBundle),这将很难正常执行。

编辑: 由@flu提及,此方法不返回true对象。但是,如果您需要来自对象的数据,它将非常有用。
然后,您可以通过某些身份从ObjectManager获取真实对象。

我们可以使用代理接口中的__load()方法

We can use __load() method from Proxy interface

$proxyObject->__load();