2

我在两个类(协议和历史)之间有一个双向的一对多关系。在搜索特定协议时,我希望看到与该协议关联的所有历史条目。

在渲染我的模板时,我传递了以下内容:

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),它可以正常工作。但我认为建立双向关系的主要目的是避免打开存储库并明确打开新的结果集。

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

4

3 回答 3

3

我所要做的就是在我的 TWIG 上调用以下命令:

{% for hist in entity.history %}

其他答案都不适合我。我必须直接在我的树枝中调用该属性,而不是使用它的 getter。不知道为什么,但它奏效了。

谢谢。

于 2013-01-08T12:02:18.170 回答
1

试试这个:

return $this->render('FunarbeProtocoloAdminBundle:Protocolo:show.html.twig'
    ,array(
          'entity'      => $entity,
         ,'delete_form' => $deleteForm->createView(),
         ,'history'     => $entity->getHistory()->toArray()
                                                ///////////
    )
);
于 2013-01-07T23:35:03.813 回答
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 %}
于 2013-01-07T14:20:41.560 回答