[英]Symfony 2 - Access mapped Object property form twig
I hava a entity with the following flied: 我有一个以下飞行的实体:
/**
* @ORM\ManyToOne(targetEntity="Document", inversedBy="posts")
* @ORM\JoinColumn(name="document_id", referencedColumnName="id")
*/
protected $image;
which I get with the following method: 我用以下方法得到:
public function indexAction() {
$posts = $this->getDoctrine()
->getRepository('airpaprcms2Bundle:Post')
->findByPageType(1);
if (!$posts) {
throw $this->createNotFoundException('No posts in Database!');
}
return $this->render('airpaprcms2Bundle:Default:index.html.twig', array('posts' => $posts));
}
How can I access a property of the mapped object form within twig? 如何在树枝内访问映射对象表单的属性? I tried
post.image.file
(the mapped entity Document has a property file) 我试过
post.image.file
(映射的实体Document有一个属性文件)
{% for post in posts %}
<div>
<h1>
<a href="{{ path('_view', {'slug': post.slug}) }}">{{ post.title }}</a>
</h1>
<p>{{ post.text }}</p>
<img src="{{ post.image.name }}" alt="{{ post.title }}" />
</div>
{% endfor %}
and get the following error message: 并收到以下错误消息:
Item "file" for "" does not exist in airpaprcms2Bundle:Default:index.html.twig at line 11
What is the right syntax to access the mapped document property? 访问映射文档属性的正确语法是什么?
You can access the property of your linked entity with the following twig syntax: 您可以使用以下twig语法访问链接实体的属性:
{% for post in posts %}
{{ post.entityName.propertyName }}
{% endfor %}
In your case that would be: 在你的情况下,将是:
{% for post in posts %}
{{ post.image.propertyName }}
{% endfor %}
Be sure to check that all post entities are linked to an image object. 务必检查所有帖子实体是否链接到图像对象。 If one post entity has no link to an image, you will get a property not found error when trying to render your page.
如果一个帖子实体没有指向图像的链接,那么在尝试呈现页面时,您将收到找不到属性的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.