目前正在处理一个Symfony2应用程序,而我在延迟加载对象方面遇到了一些问题。
我现在得到我所有的比赛
$matches = $this->getDoctrine()
->getRepository('AppBundle:Matchgame')
->findByTournament($tournament);所有的比赛都由一些细节组成,比如圆号和其他东西,同时也包含了参与者。这些参与者来自ManyToOne关系。
/**
* @ORM\ManyToOne(targetEntity="User")
*/
private $participant1;
/**
* @ORM\ManyToOne(targetEntity="User")
*/
private $participant2;当我将$matches变量传递给Twig模板时
{% 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完全加载请求吗?
谢谢一堆人!
发布于 2015-09-16 17:45:41
你可以试试:
/**
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
*/https://stackoverflow.com/questions/32614077
复制相似问题