Otonel Otonel - 5 months ago 29
PHP Question

Symfony retrieve data from Array Collection

In Symfony I am using a query to retrieve all questions and their relative answers from a database. Using a

for
in twig I am trying to display in a table all questions with the relative answers row by row. At the moment I am able to access and display the questions only but not the answers.
The relation between my entities
Questions and Answers
is (one to many) thus when retrieving the question, also the answers are retrieved. Using dump, I can see the the answers are put in an Array Collection.

Trying to get only a single question from the database with another query, using the
getAnswers()
function from my
Question
entity I am able to get the answers from the array collection using this statement:

$answer = $question->getAnswers()->getValues();


Using this statement I tried to get the answers for all the questions using the bellow code in the controller, but I get this error:


Error: Call to a member function getAnswers() on a non-object


I believe this is because the query returns multiple questions and not just one.
How can I get the answers for each individual question from the Array Collection? Thanks in advance for the help.

Controller Code:

$question = $this->getDoctrine()
->getRepository('QuizBundle:Question')
->findByIdJoinedToCategory();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $question));


Question Repository:

public function findByIdJoinedToCategory()
{
$query = $this->getEntityManager()
->createQuery(
'SELECT a, q FROM QuizBundle:Question a
JOIN a.answers q');
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}


Twig Code:

{% for item in data %}
<tbody>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.image }}</td>
<td>{{ item.question }}</td>
<td></td>
<tr>
<tbody>

{% endfor %}


Dump screenshot of the data from the query:

Query Data

Answer

With Twig and according to your doctrine query, you can display your questions/answers like this :

{% for item in data %}
    <tbody>
        <tr>
            <th scope="row">{{ item.id }}</th>
            <td>{{ item.image }}</td>
            <td>{{ item.question }}</td>
            {% for answer in item.answers %}
                {{ answer.id }}
            {% endfor %}
        <tr>
    <tbody>
{% endfor %}

Btw, if it exists a relation between the two entities Question and Answer and you want to display all the questions and their associative answers, you can directly doing this :

In your controller:

$questions = $this->getDoctrine()
            ->getRepository('QuizBundle:Question')->findAll();
return $this->render('QuizBundle:Default:admin.html.twig', array('data' => $questions));

In your Twig :

{% for question in data %}
    <tbody>
        <tr>
            <th scope="row">{{ question.id }}</th>
            <td>{{ question.image }}</td>
            {% for answer in question.answers %}
                {{ answer.id }}
            {% endfor %}
        <tr>
    <tbody>
{% endfor %}