title: Get entities from a unidirectional many to many relation with Doctrine2 and Symfony2
body: <p>I'm currently working on a language assessment project which enables you to take an exam in the language you want and evaluate your level. I use Symfony2 framework and work with Doctrine2 as well. My issue is the following one:</p>
  
  <p>I have two entities Exam and Question linked by a Many-To-Many relation (Exam being the owner). Each exam can be related to several questions, and each question can be related to several exams.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Exam entity</strong></p>
  
  <pre><code>/**
   * Exam
   *
   * @ORM\Table(name="cids_exam")
   * @ORM\Entity(repositoryClass="LA\AdminBundle\Entity\ExamRepository")
   */
  class Exam
  {
      ...
  
      /**
      * @ORM\ManyToMany(targetEntity="LA\AdminBundle\Entity\Question", cascade={"persist"})
      * @ORM\JoinTable(name="cids_exam_question")
      */
      private $questions;
  
      ...
  
  
      /**
       * Constructor
       */
      public function __construct()
      {
          $this-&gt;questions = new \Doctrine\Common\Collections\ArrayCollection();
      }
  
      /**
       * Add questions
       *
       * @param \LA\AdminBundle\Entity\Question $questions
       * @return Exam
       */
      public function addQuestion(\LA\AdminBundle\Entity\Question $questions)
      {
          $this-&gt;questions[] = $questions;
  
          return $this;
      }
  
      /**
       * Remove questions
       *
       * @param \LA\AdminBundle\Entity\Question $questions
       */
      public function removeQuestion(\LA\AdminBundle\Entity\Question $questions)
      {
          $this-&gt;questions-&gt;removeElement($questions);
      }
  
      /**
       * Get questions
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getQuestions()
      {
          return $this-&gt;questions;
      }
  }
  </code></pre>
  
  <p>As long as it is a unidirectional relation, there is no 'exams' attribute in my Question class.</p>
  
  <p>Now, what I want to do is getting all the questions related to a specific exam, calling the getQuestions() method, like this:</p>
  
  <pre><code>$questions = $exam-&gt;getQuestions();
  </code></pre>
  
  <p>But this method returns an empty array, even if I have data in my database. If I var_dump the $exam variable, I can see the questions array is empty:</p>
  
  <pre><code>object(LA\AdminBundle\Entity\Exam)[47]
    private 'id' =&gt; int 5
    ...
    private 'questions' =&gt; 
      object(Doctrine\ORM\PersistentCollection)[248]
        private 'snapshot' =&gt; 
          array (size=0)
            empty
        private 'owner' =&gt; null
        private 'association' =&gt; null
        private 'em' =&gt; null
        private 'backRefFieldName' =&gt; null
        private 'typeClass' =&gt; null
        private 'isDirty' =&gt; boolean false
        private 'initialized' =&gt; boolean false
        private 'coll' =&gt; 
          object(Doctrine\Common\Collections\ArrayCollection)[249]
            private '_elements' =&gt; 
              array (size=0)
                ...
  </code></pre>
  
  <p>I think I could maybe write a findByExam() function in my QuestionRepository, but I don't really know how to implement the joins in this case.</p>
  
  <p>Any help would be great!</p>