获得“真实"来自doctor2中代理对象的对象 [英] Getting a "true" object from a proxy object in doctrine2

查看:19
本文介绍了获得“真实"来自doctor2中代理对象的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)很难正确执行.

推荐答案

这不太可能在问题的特定实例中提供帮助,因为您依赖第三方模块,但是您可以通过以下方式防止延迟加载将您的实体的获取模式"设置为EAGER".

This is unlikely to help in the specific instance for the question, since you're relying on a third-party module, but you can prevent the lazy loading by setting the "fetch mode" for your entity to "EAGER".

User:
    ManyToOne:
        city:
            fetch: EAGER

这也可以通过注解来处理:

This can also be handled by annotations:

@ManyToOne(targetEntity="city", fetch="EAGER")
@JoinColumn(name="city", referencedColumnName="id")

参见 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-manytoone

我在这里看到的其他答案都不适合我.

None of the other answers I've seen here worked for me.

这篇关于获得“真实"来自doctor2中代理对象的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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