title: symfony3: create form for entity that has a column with many to one relationship
body: <p>I have a "Tasks" table that references a "Estados" table with a foreign key from column Tasks.taskestado to Estados.estado.</p>
  
  <p>Here's the relevant XML mapping for Tasks:</p>
  
  <pre><code>&lt;entity name="AppBundle\Entity\Tasks" table="TASKS" repository-class="AppBundle\Repository\TasksRepository"&gt;
  &lt;id name="taskid" type="bigint" column="TaskID"&gt;
    &lt;generator strategy="IDENTITY"/&gt;
  &lt;/id&gt;
  ...
  &lt;many-to-one field="taskestado" target-entity="Estados" fetch="LAZY"&gt;
    &lt;join-columns&gt;
      &lt;join-column name="TaskEstado" referenced-column-name="Estado"/&gt;
    &lt;/join-columns&gt;
  &lt;/many-to-one&gt;
  ...
  </code></pre>
  
  <p>And for Estados:</p>
  
  <pre><code>&lt;entity name="AppBundle\Entity\Estados" table="ESTADOS"&gt;
  &lt;id name="estado" type="string" column="Estado" length="15"&gt;
    &lt;generator strategy="IDENTITY"/&gt;
  &lt;/id&gt;
  &lt;field name="estadodescricao" type="string" column="EstadoDescricao" length="50" nullable="true"&gt;
    &lt;options&gt;
      &lt;option name="fixed"/&gt;
    &lt;/options&gt;
  &lt;/field&gt;
  ...
  </code></pre>
  
  <p>Given this, I'm trying to make an action (novaAction()) to create tasks.
  Here's the Controller code:</p>
  
  <pre><code>public function novaAction(Request $request)
  {
      $task = new Tasks();
      $em = $this-&gt;getDoctrine()-&gt;getManager();
  
      dump($task);
      #$task-&gt;setTaskEstado(new Estados());
      $form = $this-&gt;createForm(TasksType::class, $task);
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isSubmitted()) {
          if ($form-&gt;isValid()) {
              // Criar a tarefa na BD
              $em-&gt;persist($form-&gt;getData());
              $em-&gt;flush();
              $this-&gt;addFlash('notice', 'app.nova_tarefa.mensagem_sucesso');
  
              return $this-&gt;redirectToRoute('nova_tarefa');
          }
  
          $this-&gt;addFlash('error', 'app.nova_tarefa.mensagem_erro');
      }
  </code></pre>
  
  <p>And the relevant TasksType code:</p>
  
  <pre><code>class TasksType extends AbstractType
  {
  
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('taskid', HiddenType::class)
          ...
          #-&gt;add('taskestado')
          -&gt;add('taskestado', EntityType::class, [ 'class' =&gt; 'AppBundle:Estados' ])
          ...
  }
  
  /**
   * @param OptionsResolver $resolver
   */
  public function configureOptions(OptionsResolver $resolver)
  {
      $resolver-&gt;setDefaults(array(
          'data_class' =&gt; 'AppBundle\Entity\Tasks',
          #'empty_data' =&gt; function (\Symfony\Component\Form\FormInterface $form) {
          #    #return new Tasks($form-&gt;get('tasks')-&gt;getData());
          #    return new Tasks();
          #},
      ));
  }
  }
  </code></pre>
  
  <p>The 'empty_data' option was an attempt to create the form without passing an instance of Tasks to it.
  Also, I have the same result when I add taskestado with the commented code, i. e., without arguments.</p>
  
  <p>And here's the relevant Tasks entity:</p>
  
  <pre><code>namespace AppBundle\Entity;
  
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
   * Tasks
   */
  class Tasks
  {
      /**
       * @var integer
       */
      protected $taskid;
  
      /**
       * @var \AppBundle\Entity\Estados
       */
      protected $taskestado;
  
      ...
  
      /**
       * Get taskid
       *
       * @return integer
       */
      public function getTaskid()
      {
          return $this-&gt;taskid;
      }
  
      /**
       * Set taskestado
       *
       * @param \AppBundle\Entity\Estados $taskestado
       *
       * @return Tasks
       */
      public function setTaskestado(\AppBundle\Entity\Estados $taskestado = null)
      {
          $this-&gt;taskestado = $taskestado;
  
          return $this;
      }
  
      /**
       * Get taskestado
       *
       * @return \AppBundle\Entity\Estados
       */
      public function getTaskestado()
      {
          return $this-&gt;taskestado;
      }
  
      ...
  }
  </code></pre>
  
  <p>When I open the page, I get the following error:</p>
  
  <blockquote>
    <p>Entities passed to the choice field must be managed. Maybe persist them in the entity manager?</p>
  </blockquote>
  
  <p>I'd also generated the CRUD (<code>bin/console doctrine:generate:crud --filter=Tasks</code>), so that I could check how it's done, but the code is similar, as well as the results (after fixing some issues in the TasksType for some datetime columns).</p>
  
  <p>What am I doing wrong?</p>