title: Troubleshooting "Entities passed to the choice field must be managed"
body: <p>This question has been asked several times on SO - and each time, the solution appears to be different, and almost always, the answer appears to have been discovered by chance/ or it is a crude hack - meaning  that there is no consistent view as to what causes the problem and how it can be fixed.</p>
  
  <p>I am also encountering the message <strong>"Entities passed to the choice field must be managed"</strong> - and it is not clear why this exception is being raised.</p>
  
  <p>I have a Contact class defined (.yml format) in a bundle. The Contact class has a manyToOne relation ship with two other classes <code>Promotion</code> and <code>ContactReferrer</code> - see below:</p>
  
  <pre><code>Foobar\ContactlistBundle\Entity\Contact:
      type: entity
      table: contact
      repositoryClass: Foobar\ContactlistBundle\Repository\ContactRepository
  
      id:
          id:
              type: integer
              generator: { strategy: AUTO }
      fields:
          first_name:
              type: string
              length: 32
              nullable: true
  
          last_name:
              type: string
              length: 64
              nullable: true
  
          email:
              type: string
              length: 128
              unique: true
  
          token:
              type: string
              length: 8
              unique: true
  
          is_validated:
              type: boolean
  
          created_at:
              type: datetime
  
          updated_at:
              type: datetime
              nullable: true
  
  
      manyToOne:
          promotion:
              targetEntity: Promotion
              inversedBy: promoted_contacts
              joinColumn:
                  name: promotion_id
                  referencedColumnName: id
  
          referrer:
              targetEntity: ContactReferrer
              inversedBy: referrer_contacts
              joinColumn:
                  name: contact_referrer_id
                  referencedColumnName: id
  </code></pre>
  
  <p>I generated the form using <code>php app/console doctrine:generate:form FooBarContaclistBundleContact</code> and manually edited the form as follows:</p>
  
  <pre><code>class ContactType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder
              -&gt;add('first_name')
              -&gt;add('last_name')
              -&gt;add('email', 'email')
              -&gt;add('token')
              -&gt;add('is_validated')
              -&gt;add('created_at')
              -&gt;add('updated_at')
              -&gt;add('promotion', 'entity', array(
                                                  'class' =&gt; 'Foobar\ContactlistBundle\Entity\Promotion',
                                                  'expanded' =&gt; false,
                                                  'multiple' =&gt; false, )
                   )
              -&gt;add('referrer', 'entity', array(
                                                  'class' =&gt; 'Foobar\ContactlistBundle\Entity\ContactReferrer',
                                                  'expanded' =&gt; false,
                                                  'multiple' =&gt; false, )
              );
      }
  
      public function getName()
      {
          return 'Foobar_contactlistbundle_contacttype';
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'data_class' =&gt; 'Foobar\ContactlistBundle\Entity\Contact',
          );
      }
  }
  </code></pre>
  
  <p>In my Entity/Contact.php class, here is the code for the constructor:</p>
  
  <pre><code>class Contact
  {
      public function __construct()
      {
          $this-&gt;token = 'abcdef123';
          $this-&gt;is_validated = false;
          $this-&gt;created_at =  new \DateTime();
          $this-&gt;setUpdatedAt(new \DateTime());
  
          // Set these to defaults
          $this-&gt;promotion = new \Foobar\ContactlistBundle\Entity\Promotion();
          $this-&gt;referrer = new \Foobar\ContactlistBundle\Entity\ContactReferrer();
      }
  
      // more code ... 
  }
  </code></pre>
  
  <p>In my controller, I have the following code:</p>
  
  <pre><code>public function newcontactAction(Request $request)
  {
      // do something ...
      $contact = new Contact();
      $form = $this-&gt;createForm(new ContactType(), $contact);  // &lt;- Exception thrown here
      // do something ...
  }
  </code></pre>
  
  <p>When I browse the route that causes the controller code above to be executed, I get the error: <code>Entities passed to the choice field must be managed</code> - Does <strong>anyone</strong> know what is causing this and how to fix it?</p>