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

Php Symfony 2-如何在细枝中打印属性?,php,symfony,twig,Php,Symfony,Twig,它在PhotoBundle中有两个实体,一个是Photo实体,另一个是FileManaged实体。他们有一对一的关系 Hy\PhotoBundle\Entity\Photo: type: entity oneToOne: file_managed: targetEntity: FileManaged mappedBy: photo joinColumn: name: photo referencedColumnName:

它在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 %}
我做错了什么

pic正在使用瓢虫转储:

{{ entity|ladybug_dump }}

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

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或您在细枝模板中的实体中定义的任何内容。

您的PhotoEntity中是否有getFile_managed()函数?如果是对象而不是阵列,请尝试使用fileManaged而不是file_managed()函数为什么需要getFile_managed()函数?它可以使用{entity | ladybug_dump}来显示数据。如果我使用fileManaged而不是file_managed,它会显示:Hy\PhotoBundle\entity\Photo对象“Hy\PhotoBundle\Photo”的方法“fileManaged”在HyPhotoBundle中不存在:Photo:index.html.twig在PhotoEntity的第25行您应该有setFileManaged和getFileManaged,getFileManaged返回相关的FileManaged对象。您的FileManaged实体是否有一个名为url的字段?
class Photo{

   protected $id;

   protected $file_managed;

   public function setFileManaged($file)
   {

       $this->file_managed = $file;

       return $this;

   }

   public function getFileManaged()
   {

       return $this->file_managed;

   }

}