title: Symfony Form Events, but "data" not part of form's entity
body: <p>I'm trying to do the following process for a dynamic form:</p>
  
  <ol>
  <li>The user selects a school</li>
  <li>I then need to get all the "grades" linked to that school. </li>
  </ol>
  
  <p>What I'm trying to achieve with this form is create a new "Class" (as in school class), which is linked to a grade. The issue (I think and as you'll see below) is that the SchoolClass object doesn't know about the School object, only about the Grade object. I'll post only the relevant code...</p>
  
  <p>School class</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Grade", mappedBy="school")
   */
  private $grades;
  public function __construct()
  {
      $this-&gt;grades = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Add grades
   *
   * @param \MyBundle\MainBundle\Entity\Grade $grades
   * @return School
   */
  public function addGrade(\MyBundle\MainBundle\Entity\Grade $grades)
  {
      $this-&gt;grades[] = $grades;
  
      return $this;
  }
  
  /**
   * Remove grades
   *
   * @param \MyBundle\MainBundle\Entity\Grade $grades
   */
  public function removeGrade(\MyBundle\MainBundle\Entity\Grade $grades)
  {
      $this-&gt;grades-&gt;removeElement($grades);
  }
  
  /**
   * Get grades
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getGrades()
  {
      return $this-&gt;grades;
  }
  </code></pre>
  
  <p>Grade class</p>
  
  <pre><code>/**
   * @var School
   *
   * @ORM\ManyToOne(targetEntity="School", inversedBy="grades")
   * @ORM\JoinColumn(name="school_id", referencedColumnName="id")
   */
  private $school;
  
  /**
   * @ORM\OneToMany(targetEntity="SchoolClass", mappedBy="grade")
   */
  private $schoolClasses;
  
  //Getters and setters for school come here, leaving them out as they are the usual...
  
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this-&gt;schoolClasses = new \Doctrine\Common\Collections\ArrayCollection();
  }
  
  /**
   * Get schoolClasses
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getSchoolClasses()
  {
      return $this-&gt;schoolClasses;
  }
  
  /**
   * Add schoolClasses
   *
   * @param \MyBundle\MainBundle\Entity\SchoolClass $schoolClasses
   * @return Grade
   */
  public function addSchoolClasse(\MyBundle\MainBundle\Entity\SchoolClass $schoolClasses)
  {
      $this-&gt;schoolClasses[] = $schoolClasses;
  
      return $this;
  }
  /**
   * Remove schoolClasses
   *
   * @param \MyBundle\MainBundle\Entity\SchoolClass $schoolClasses
   */
  public function removeSchoolClasse(\MyBundle\MainBundle\Entity\SchoolClass $schoolClasses)
  {
      $this-&gt;schoolClasses-&gt;removeElement($schoolClasses);
  }
  </code></pre>
  
  <p>By the way, the <code>removeSchoolClasse()</code> and <code>addSchoolClasse()</code> functions are generated like that by Doctrine, is this a bug? </p>
  
  <p>EDIT - forgot to add the SchoolClass class...</p>
  
  <pre><code>/**
   * @var Grade
   *
   * @ORM\ManyToOne(targetEntity="Grade", inversedBy="schoolClasses")
   * @ORM\JoinColumn(name="grade_id", referencedColumnName="id")
   */
  private $grade;
  /**
   * Set grade
   *
   * @param Grade $grade
   * @return SchoolClass
   */
  public function setGrade($grade)
  {
      $this-&gt;grade = $grade;
  
      return $this;
  }
  
  /**
   * Get gradeId
   *
   * @return integer
   */
  public function getGrade()
  {
      return $this-&gt;grade;
  }
  </code></pre>
  
  <p>Anyway, I tried following the tutorial on the Symfony docs, and that didn't work, so I tried my own implmentation for the form event, the important parts of which are here:</p>
  
  <pre><code>    $builder
              -&gt;add('classDescription')
              -&gt;add('school', 'entity', array(
                  'class' =&gt; 'MyBundleMainBundle:School',
                  'multiple' =&gt; false,
                  'mapped' =&gt; false,
              ));
  
          //Add event listener for when user changes the school...
          $builder-&gt;addEventListener(
              FormEvents::PRE_SET_DATA,
              function(FormEvent $event) {
                  $form = $event-&gt;getForm();
                  //Here I tried my own implementation but obviously doesn't work as $school will be of type Form, am I correct?
                  $school = $form-&gt;get('school');
                  $grades = $school-&gt;getGrades();
  
                  $form-&gt;add('grade', 'entity', array(
                      'choices' =&gt; $grades,
                  ));
              }
          );
  </code></pre>
  
  <p>Any help would be highly appreciated!</p>