2015-09-16 52 views
0

當前正在使用Symfony2應用程序,並且在對象的延遲加載中遇到了一些麻煩。在Twig中訪問鏈接的對象

我目前得到的所有我的比賽有

$matches = $this->getDoctrine() 
     ->getRepository('AppBundle:Matchgame') 
     ->findByTournament($tournament); 

所有比賽的包括一些細節,如roundnumber之類的東西,並擁有參與者。這些參與者來自ManyToOne關係。

/** 
    * @ORM\ManyToOne(targetEntity="User") 
    */ 
    private $participant1; 

    /** 
    * @ORM\ManyToOne(targetEntity="User") 
    */ 
    private $participant2; 

當我通過$匹配變量,以我的枝條模板

{% for match in matches %} 
     <p>{{ match.participant1.username }}</p> 
    {% endfor %} 

,然後嘗試訪問的用戶名,我得到的錯誤

Impossible to access an attribute ("username") on a null variable in tournament/single.html.twig at line 46 

就像我說的,這是可能是因爲延遲加載。但是有沒有解決方法?換句話說,我可以告訴Doctrine完全加載請求嗎?

非常感謝!

回答

1

你可以試試:

/** 
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER") 
*/ 
+0

完美地解決了我的問題,謝謝! – Neglexis