2013-01-07 41 views
2

我在两个类(Protocol和History)之间有双向的一对多关系。在搜索特定的协议时,我期望看到与该协议相关的所有历史条目。在Twig中读取PersistentCollections,就好像它们是数组一样

虽然渲染我的模板,我通过了以下内容:

return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig', array(
     'entity'  => $entity, 
     'delete_form' => $deleteForm->createView(), 
     'history' => $entity->getHistory(), 
    ) 
); 

entity->getHistory()返回PersistentCollection不是数组,这会导致下面的渲染错误:的

{% for hist in history %} 
<tr> 
    <td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td> 
    <td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td> 
</tr> 
{% endfor %} 

相反,如果$entity->getHistory()我通过$em->getRepository('MyBundle:History')->findByProtocol($entity),它工作正常。但我认为建立双向关系的主要目的是避免打开存储库并显式打开新的结果集。

我做错了什么?我该怎么做?

回答

3

我所要做的就是呼吁我的TWIG如下:

{% for hist in entity.history %} 

其他的无答案为我工作。我必须直接在我的树枝中调用该属性,而不是使用它的getter。不知道为什么,但它工作。

谢谢。

0

你的代码没问题,我总是把自己的麻烦省下来,把我的藏品放在树枝里,而不是通过我的视图。你也可以试试这个。

渲染代码更改

return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig', array(
     'entity'  => $entity, 
     'delete_form' => $deleteForm->createView(), 
    ) 
); 

你想直接访问历史的树枝。

嫩枝

{% for hist in entity.getHistory() %} 
    <tr> 
     <td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td> 
     <td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td> 
    </tr> 
{% endfor %} 

如果变更后的结果是一样的,尝试检查一个数组HIST,它可以被嵌套!持久化集合倾向于做...

{% for history in entity.getHistory() %} 
    {% for hist in history %} 
     <tr> 
      <td>{{ hist.dtOcorrencia|date('d/m/Y H:i') }}</td> 
      <td>{{ hist.dtRetorno|date('d/m/Y H:i') }}</td> 
     </tr> 
    {% endfor %} 
{% endfor %} 
1

试试这个:

return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig' 
    ,array(
      'entity'  => $entity, 
     ,'delete_form' => $deleteForm->createView(), 
     ,'history'  => $entity->getHistory()->toArray() 
               /////////// 
    ) 
); 
相关问题