避免懒惰加载教条Symfony2 [英] Avoid lazy loading Doctrine Symfony2

查看:183
本文介绍了避免懒惰加载教条Symfony2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有两个实体:用户和头像。



用户拥有具有OneToOne关系的头像。



头像是具有文件对象和fileName的实体。它使用@ ORM\HasLifecycleCallbacks保存文件或按照 Symfony2文档



在我的控制器中,我想从当前用户中删除阿凡达实体(我使用 $ currentUser = $ this-> get('security.context') - > getToken() - > getUser()),但是我不能使用 $ currentUser-> getAvatar()

  var_dump($ currentUser-> ; getAvatar());出口; 

输出:

 code> object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)$ 355 
public'__initializer__'=>
object(Closure)$ 348 $ b public'__cloner__'=>
object(Closure)[349]
public'__isInitialized__'=> boolean false
private'id'(Participso\UserBundle\Entity\Avatar)=> int 20
public'file'=> null
private'fileName'(Participso\UserBundle\Entity\Avatar)=> null

但如果我这样做

  $ whatever = $ currentUser-> getAvatar() - > getFileName(); 
var_dump($ currentUser-> getAvatar()); exit;

输出:

 code> object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)$ 355 
public'__initializer__'=>
object(Closure)$ 348 $ b public'__cloner__'=>
object(Closure)[349]
public'__isInitialized__'=> boolean false
private'id'(Participso\UserBundle\Entity\Avatar)=> int 20
public'file'=> null
private'fileName'(Participso\UserBundle\Entity\Avatar)=>字符串'd4e5eadd3757498a22b14ad1f81869c2baf459d3.png'

这是很讨厌...有人有一个线索来避免这个

解决方案

Doctrine docs ,你只需要指定提取行为就是渴望。

  / ** 
* @OneToOne(targetEntity =User)
* @JoinColumn(name =user_id,referencedColumnName =id,fetch =EAGER)
* /

YAML或其他配置示例。


I have two entities in my project : User and Avatar.

User owns Avatar with a OneToOne relation.

Avatar is an entity with a file object and a fileName. It uses @ORM\HasLifecycleCallbacks to save the file or to remove it as described in the Symfony2 documentation.

In my controller, I want to remove the Avatar entity from the current user (i use $currentUser = $this->get('security.context')->getToken()->getUser()), but I can't get the avatar with $currentUser->getAvatar() :

var_dump($currentUser->getAvatar());exit;

Output :

object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    public '__initializer__' =>
object(Closure)[348]
    public '__cloner__' =>
object(Closure)[349]
    public '__isInitialized__' => boolean false
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    public 'file' => null
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => null

But if i do

$whatever = $currentUser->getAvatar()->getFileName();
var_dump($currentUser->getAvatar());exit;

Output :

object(Proxies\__CG__\Participso\UserBundle\Entity\Avatar)[355]
    public '__initializer__' =>
object(Closure)[348]
    public '__cloner__' =>
object(Closure)[349]
    public '__isInitialized__' => boolean false
    private 'id' (Participso\UserBundle\Entity\Avatar) => int 20
    public 'file' => null
    private 'fileName' (Participso\UserBundle\Entity\Avatar) => string 'd4e5eadd3757498a22b14ad1f81869c2baf459d3.png'

This is pretty annoying... Does somebody have a clue to avoid this ?

解决方案

As described in the Doctrine docs, you just need to specify the fetching behavior to be eager.

/**
 * @OneToOne(targetEntity="User")
 * @JoinColumn(name="user_id", referencedColumnName="id", fetch="EAGER")
 */

See the documentation for YAML or other configuration examples.

这篇关于避免懒惰加载教条Symfony2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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