Symfony EntityRepository 返回“Proxies\__CG__ MyModelName"的实例 [英] Symfony EntityRepository return instance of "Proxies\__CG__ MyModelName"

查看:23
本文介绍了Symfony EntityRepository 返回“Proxies\__CG__ MyModelName"的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查询

$em->getRepository($this->getRepositoryName('AppBundle:User'))->find($id);

返回 Proxies\__CG__AppBundleEntityUser 的对象实例,而不是 AppBundleEntityUser.这是什么原因?

return object instance of Proxies\__CG__AppBundleEntityUser instead of AppBundleEntityUser. What the reason of this?

推荐答案

Doctrine 为您提供了一个来自自动生成的类的代理对象,该类扩展了您的实体并实现了 DoctrineORMProxyProxy.您可以在 app/cache/dev/doctrine/orm/Proxies/ 中查看这些自动生成的类的代码.

Doctrine is giving you a proxy object from an auto-generated class that extends your entity and implements DoctrineORMProxyProxy. You can view the code for these auto-generated classes in app/cache/dev/doctrine/orm/Proxies/.

代理对象允许 Doctrine 提供的一组行为,否则您必须在实体中显式编码,包括对属性延迟加载的支持.例如,如果您的对象具有对另一个实体的引用(例如来自 OneToOne/OneToMany/ManyToOne/ManyToMany 关联),您不一定要在检索 User 记录时始终加载这些引用,因为它们可能并不总是相关的.延迟加载允许稍后按需引入数据.

The proxy object allows for a set of behaviors that Doctrine provides that you would otherwise have to explicitly code into your entity, including support for lazy-loading of properties. For example, if your object has a reference to another entity (such as from a OneToOne/OneToMany/ManyToOne/ManyToMany association), you don't necessarily want to always load those references when you retrieve your User record, because they may not be relevant all the time. Lazy-loading allows that data to be brought in later on-demand.

为了执行延迟加载,实体需要访问 Doctrine 以便它可以要求 Doctrine 检索相关数据.这是通过提供给代理对象的 __initializer__ 属性完成的.剩下的事情就会发生,由 Doctrine 处理,而您的代码不需要知道细节.

In order to perform that lazy loading, the entity needs to have access to Doctrine so it can ask Doctrine to retrieve the relevant data. This is done through an __initializer__ property that is provided to the proxy object. The rest then happens, handled by Doctrine, without your code needing to know the details.

这篇关于Symfony EntityRepository 返回“Proxies\__CG__ MyModelName"的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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