Symfony 2 - 如何在树枝上打印属性?

时间:2015-10-23 10:27:38

标签: php symfony twig

PhotoBundle中有两个实体,一个是Photo实体,另一个是FileManaged实体。他们有一对一的关系。

Hy\PhotoBundle\Entity\Photo:
type: entity
oneToOne:
    file_managed:
      targetEntity: FileManaged
      mappedBy: photo
      joinColumn:
        name: photo
        referencedColumnName: fid

Hy\PhotoBundle\Entity\FileManaged:
type: entity
oneToOne:
    photo:
      targetEntity: Photo
      joinColumn:
        name: fid
        referencedColumnName: photo

我想在照片的index.html.twig文件中打印uri,如何打印?

我的代码是:

{% for entity in pagination %}
{{ entity.title }} <!--Ok-->
{{ entity.file_managed.uri }} <!--Error-->{% endfor %}

并显示错误消息:

Method "file_managed" for object "Hy\PhotoBundle\Entity\Photo" does not exist in HyPhotoBundle:Photo:index.html.twig at line 25

我做错了什么?

pic正在使用瓢虫转储:

{{ entity|ladybug_dump }}

http://i.stack.imgur.com/kxu8X.png

1 个答案:

答案 0 :(得分:0)

您的PhotoBundle \ Entity \ Photo实体应如下所示。请注意,它没有完全充实,只是相关的方法。

maxlength

使用此功能,您应该能够访问entity.fileManaged.url或您在twig模板中实体中定义的任何内容。