简体   繁体   中英

Symfony 2 - How to print properties in twig?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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