我在两个类(协议(protocol)和历史记录)之间存在双向一对多关系。在搜索特定协议(protocol)时,我希望看到与该协议(protocol)关联的所有历史条目。
在呈现我的模板时,我传递了以下内容:
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 %}
如果我传递 $em->getRepository('MyBundle:History')->findByProtocol($entity)
而不是 $entity->getHistory()
,它工作正常。但我认为建立双向关系的主要目的是避免打开存储库并明确打开新的结果集。
我做错了什么吗?我应该怎么做?
最佳答案
我所要做的就是在我的 TWIG 上调用以下命令:
{% for hist in entity.history %}
其他答案都不适合我。我必须直接在我的 Twig 中调用该属性,而不是使用它的 getter。不知道为什么,但它奏效了。
谢谢。
关于php - 像读取数组一样读取 Twig 中的持久集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14197393/