Symfony EntityRepository return instance of “Proxies\__CG__ MyModelName”

后端 未结 2 1588
广开言路
广开言路 2021-02-20 06:37

Query

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

return object instance of Proxies\\__CG__\\A

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-20 07:00

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

    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.

    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.

提交回复
热议问题