Symfony EntityRepository返回“Proxies\__CG__ MyModelName”的实例 [英] Symfony EntityRepository return instance of "Proxies\__CG__ MyModelName"
问题描述
查询
$em->getRepository($this->getRepositoryName('AppBundle:User'))->find($id);
返回对象实例 Proxies\__CG__\AppBundle\Entity\\ \\用户
而不是 AppBundle\Entity\User
。这是什么原因?
return object instance of Proxies\__CG__\AppBundle\Entity\User
instead of AppBundle\Entity\User
. What the reason of this?
推荐答案
原则是给你一个代理对象从一个自动生成的类,扩展你的实体和实现 \Doctrine\ORM\Proxy\Proxy
。您可以在 app / cache / dev / doctrine / orm / Proxies /
中查看这些自动生成的类的代码。
Doctrine is giving you a proxy object from an auto-generated class that extends your entity and implements \Doctrine\ORM\Proxy\Proxy
. You can view the code for these auto-generated classes in app/cache/dev/doctrine/orm/Proxies/
.
代理对象允许Doctrine提供的一组行为,否则您必须明确地编码到您的实体中,包括对延迟加载属性的支持。例如,如果您的对象引用另一个实体(例如从 OneToOne
/ OneToMany
/ ManyToOne
/ ManyToMany
关联),当您检索到时,您不一定要总是加载这些引用用户
记录,因为它们可能并不相关。延迟加载允许随后按需提供数据。
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才能询问原则来检索相关数据。这是通过提供给代理对象的 __ 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屋!