title: Symfony retrieve data from Array Collection
body: <p>In Symfony I am using a query to retrieve all questions and their relative answers from a database. Using a <code>for</code> 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 <code>Questions and Answers</code> 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.</p>
  
  <p>Trying to get only a single question from the database with another query, using the <code>getAnswers()</code> function from my <code>Question</code> entity I am able to get the answers from the array collection using this statement:</p>
  
  <pre><code>$answer = $question-&gt;getAnswers()-&gt;getValues();
  </code></pre>
  
  <p>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: </p>
  
  <blockquote>
    <p>Error: Call to a member function getAnswers() on a non-object</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Controller Code:</p>
  
  <pre><code>$question = $this-&gt;getDoctrine()
              -&gt;getRepository('QuizBundle:Question')
              -&gt;findByIdJoinedToCategory();
  return $this-&gt;render('QuizBundle:Default:admin.html.twig', array('data' =&gt; $question));
  </code></pre>
  
  <p>Question Repository:</p>
  
  <pre><code>public function findByIdJoinedToCategory()
      {
          $query = $this-&gt;getEntityManager()
              -&gt;createQuery(
                  'SELECT a, q FROM QuizBundle:Question a
                  JOIN a.answers q');
          try {
              return $query-&gt;getResult();
          } catch (\Doctrine\ORM\NoResultException $e) {
              return null;
          }
      }
  </code></pre>
  
  <p>Twig Code:</p>
  
  <pre><code>{% for item in data %}
                &lt;tbody&gt;
                  &lt;tr&gt;
                      &lt;th scope="row"&gt;{{ item.id }}&lt;/th&gt;
                      &lt;td&gt;{{ item.image }}&lt;/td&gt;
                      &lt;td&gt;{{ item.question }}&lt;/td&gt;
                      &lt;td&gt;&lt;/td&gt;
                  &lt;tr&gt;
                 &lt;tbody&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>Dump screenshot of the data from the query:</p>
  
  <p><a href="https://i.stack.imgur.com/2DJfq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2DJfq.jpg" alt="Query Data"></a></p>