访问 Twig 中的链接对象
Access linked object in Twig
目前正在开发一个 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 完全加载请求吗?
非常感谢!
你可以试试:
/**
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
*/
目前正在开发一个 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 完全加载请求吗?
非常感谢!
你可以试试:
/**
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
*/