[英]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 }}
您的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.