我在两个类(协议和历史)之间有一个双向的一对多关系。在搜索特定协议时,我希望看到与该协议关联的所有历史条目。
在渲染我的模板时,我传递了以下内容:
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()
I pass $em->getRepository('MyBundle:History')->findByProtocol($entity)
,它可以正常工作。但我认为建立双向关系的主要目的是避免打开存储库并明确打开新的结果集。
难道我做错了什么?我该怎么做?