如何从模板中相关实体获取属性


Howto get attribute from related entity in template

我想从使用"延迟加载"的实体中获取id属性。这是我的控制器:

public function indexAction() {
    $em = $this->getDoctrine()->getManager();
    $topics = $em->getRepository('BackendBundle:Topic')->findAll();
    $posts = $em->getRepository('BackendBundle:Post')->findAll();
    return $this->render('BackendBundle:Home:home.html.twig', 
        ['topics' => $topics, 'posts' => $posts]
    );
}

这是我的模板块:

{% block article %}
    {% for post in posts %}
        <h3>
            <a href="{{ path('backend_posts_post', 
                    { 'topic_id': post.topic.id, 'post_id': post.id }) }}">
                    {{ post.title }}
            </a>
        </h3>
        <br>
        <p>{{ post.text }}</p>
        <hr>
    {% endfor %}
{% endblock %}

我正在尝试获得主题id。每个帖子都依赖于一个主题,应该可以获得他的标识符。

解决。我试图访问一个没有主题的帖子。