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 基于OPENCV的人脸识别
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!