doucheng9058 2015-10-23 10:27
浏览 17
已采纳

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

It have two entity in PhotoBundle, one is Photo entity, another one is FileManaged entity. they have ONE-TO-ONE relation.

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

I want to print uri in photo's index.html.twig file, how to print it?

My Code is:

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

and it show an error message:

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

What I'm doing wrong?

the pic is using ladybug dump:

{{ entity|ladybug_dump }}

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

  • 写回答

1条回答 默认 最新

  • donglvlao8367 2015-10-23 10:49
    关注

    Your PhotoBundle\Entity\Photo entity should look like this. Note it is not fully fleshed out, just the relevant methods.

    class Photo{
    
       protected $id;
    
       protected $file_managed;
    
       public function setFileManaged($file)
       {
    
           $this->file_managed = $file;
    
           return $this;
    
       }
    
       public function getFileManaged()
       {
    
           return $this->file_managed;
    
       }
    
    }
    

    With this you should be able to access entity.fileManaged.url or whatever you defined in your entity inside your twig templates.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号