php - 像读取数组一样读取 Twig 中的持久集合

标签 php symfony doctrine-orm twig

我在两个类(协议(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/

相关文章:

php - 在 Windows 上的 file_exists() 中使用 DIRECTORY_SEPARATOR

PHP和C++汉字倒序UTF-8编码单元

Symfony2 在响应监听器中获取当前环境

doctrine-orm - 在Doctrine 2.2中体现MySQL多态关联

symfony - 原则 2 使用选项数组搜索数组字段

php - 在搜索引擎上提交时如何删除 URL 参数

php - "Cannot log in to the MySQL server",没有在 CentOS7 上使用 phpMyAdmin 的代码

javascript - 将修改后的 json 从 View 发送到 Controller

mysql - "SQLSTATE[23000]: Integrity constraint violation"具有有效约束

postgresql - 如何正确使用查询构建器来进行子选择?