繁体   English   中英

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM