我从渲染控制器方法调用的控制器中获取对象时遇到问题。
这是我的实体,具有自我OneToOne关系:
class Family
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="Family")
* @ORM\JoinColumn(name="brother_id", referencedColumnName="id")
**/
private $brother;
/**
* @ORM\Column(type="string", length=100)
*/
private $label;
}
这是我的行动:
/**
* @Template()
*/
public function testAction()
{
$em = $this->getDoctrine()->getManager();
$brothers = $em->getRepository('FifaAdminBundle:Family')->findAll();
return array(
'brothers' => $brothers,
);
}
我的观点
{% for brother in brothers %}
{{ brother.id }} - {{ brother.label }}
<hr />
{% render controller('AdminBundle:Test:show', {'brother': brother}) %}
<hr />
{{ render(controller('AdminBundle:Test:show', { 'brother': brother })) }}
<hr />
{% endfor %}
我的其他控制器
public function showAction($brother)
{
if (is_object($brother))
{
return new \Symfony\Component\HttpFoundation\Response('OK');
}
else
{
var_dump($brother);
return new \Symfony\Component\HttpFoundation\Response('KO');
}
}
第一个元素是好的。 但如果它有一个brother_id,这个兄弟就不会被showAction加载。
它给了我这个:
array(1) { ["__isInitialized__"]=> string(1) "1" }
请帮帮我。
答案 0 :(得分:1)
您可能希望在案例中使用@ParamConverter
注释。
您的控制器将如下所示:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Admin\Bundle\Entity\Family;
/**
* @Route("/show/{id}")
* @ParamConverter("family", class="AdminBundle:Family")
*/
public function showAction(Family $brother)
{
//Do your stuff
}
观点:
{% for brother in brothers %}
{{ brother.id }} - {{ brother.label }}
<hr />
{{ render(controller('AdminBundle:Test:show', { 'brother': brother.id })) }}
<hr />
{% endfor %}
请注意,如果未找到Family
个对象,则会生成404响应。因此,您无需检查控制器中是否有$brother
对象。
http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
答案 1 :(得分:0)
谢谢cheesemacfly
确实,它与@ParamConverter一起使用
但是它很糟糕,因为如果我删除OneToOne关系,它可以在没有@ParamConverter的情况下工作