我在兩個類(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)
,它工作正常。但我認爲建立雙向關係的主要目的是避免打開存儲庫並顯式打開新的結果集。
我做錯了什麼?我該怎麼做?