I have 2 tables topics and comments: In the comments table a have column named topic_id where the id is the number, which corresponds to the commented topic from the user. I list the topic details with the following function in the TopicController:
/**
* @Route("/topic/{id}", name="topic_details")
* @param $id
* @return \Symfony\Component\HttpFoundation\Response
*/
public function topicDetailsAction($id)
{
$topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
return $this->render('topics/topic.details.html.twig', array(
'topic' => $topic
));
}
Now im trying to display the comments for the current selected topic with this function in the CommentController:
/**
* @Route("/topic/{id}", name="topic_details")
* @return \Symfony\Component\HttpFoundation\Response
*/
public function listCommentsAction($id)
{
$topic = $this->getDoctrine()->getRepository('AppBundle:Topic')->find($id);
$topicComments = $topic->getComments();
return $this->render('topics/topic.details.html.twig', array(
'topicComments' => $topicComments
));
}
After all, when i try to print all the data from specific topic in the twig, i got the following exception:
Variable "topicComments" does not exist. Im sure the problem isn't big and can be solved, but not sure what i'm missing. Here's my twig template:
{% extends 'base.html.twig' %}
{% block body %}
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title"><a href="/topic/{{ topic.id }}">{{ topic.title }}</a></h3>
</div>
<br>
<div class="container">
<h3>Description:</h3>
<div class="well">
<div class="panel-body">
{{ topic.description }}
</div>
</div>
</div>
<hr>
<div class="panel-body">
<div class="well">
<b>
Category: {{ topic.category }} <br>
Created: {{ topic.dateCreated|date("m/d/Y H:i:s") }}
</b>
</div>
</div>
</div>
<div class="container">
<div class="panel panel-primary">
<div class="panel-body">
</div>
</div>
</div>
<a href="/{{ topic.id }}/comment/add" class="btn btn-lg btn-default">Leave a Comment</a>
</div>
<div class="container">
{% for comment in topicComments %}
{{ comment.description }}
{% endfor %}
</div>
{% endblock %}