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

Symfony 2 - How to print properties in 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

我做错了什么?

图片使用的是瓢虫转储:

{{ entity|ladybug_dump }}

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

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

class Photo{

   protected $id;

   protected $file_managed;

   public function setFileManaged($file)
   {

       $this->file_managed = $file;

       return $this;

   }

   public function getFileManaged()
   {

       return $this->file_managed;

   }

}

有了这个,您应该能够访问 entity.fileManaged.url 或您在 twig 模板中的实体中定义的任何内容。