2013-06-18 35 views
0

我有一个问题,从render控制器方法调用的控制器中获取对象。Symfony2错误渲染控制器的关系OneToOne在同一张桌子上

这是我的实体与自我OneToOne关系:

​​

这是我的行动:

/** 
* @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" } 

请帮助我。

回答

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

0

谢谢cheesemacfly

事实上,它的工作与@ParamConverter

但它是wird因为如果我删除OneToOne关系,它的工作原理没有@ParamConverter