title: Symfony 2.3 pass custom data to entity form, using choice or other type
body: <p><strong>SETUP:</strong><br /></p>
  
  <ul>
  <li>Main entity with a related entity with ManyToOne relation.<br /> </li>
  <li>Main entity has a formType with the related entity added.<br /></li>
  <li>The related entity is a big object with a lot of fields and related objects, and very slow to get.<br /></li>
  </ul>
  
  <blockquote>
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
        -&gt;add('relatedEntity', 'entity', array(
            'class' =&gt; 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity',
            'query_builder' =&gt; function (EntityRepository $er) {
                    $queryBuilder = $er-&gt;createQueryBuilder('relatedEntity');
                    $queryBuilder-&gt;resetDQLPart('select');
                    $queryBuilder-&gt;resetDQLPart('from');
                    $queryBuilder-&gt;select('relatedEntity')
                        -&gt;distinct(true)
                        -&gt;from('ProjectNameRelatedEntityBundle:RelatedEntity', 'relatedEntity');
                    return $queryBuilder;
                },
         ....
         ....
  }
  </code></pre>
  </blockquote>
  
  <p><br />
  <strong>Template:</strong><br />
  (relateEntity has a <code>__toString()</code> function defined to show its name).<br /></p>
  
  <blockquote>
  <pre><code>{{ form_label(form.relatedEntity) }}
  {{ form_widget(form.relatedEntity) }}
  {{ form_errors(form.relatedEntity) }}
  </code></pre>
  </blockquote>
  
  <p><br />
  <strong>QUESTIONS:</strong><br /></p>
  
  <ol>
  <li>The Main entity as shown above, will get all objects and pass them
  to the template. It works perfectly but it is very slow since the
  related entity objects are big and the query may take more 10
  seconds to finish hydrating all the object data.<br /></li>
  <li><strong>How could I select only some fields from my related entity and show them in the template without getting all objects hydrated?</strong><br/></li>
  <li>Is it possible to use the choice option or another type instead of
  the default <code>entity</code> type to get only some fields of the related
  entity and show them in the template?<br /></li>
  <li>How could I build a custom query hydrated as a simple array of key value, and pass that array to the formType, to the queryBuilder of my related entity field?</li>
  <li>Finally, in case its not possible to get only some fields to be
  shown in the template,  should I avoid symfony 2 forms and make a
  custom management of the related entity?<br/></li>
  </ol>
  
  <p><br /><br />
  <strong>TESTS:</strong><br />
  I cant seem to build the form with the <a href="http://symfony.com/doc/current/reference/forms/types/choice.html" rel="nofollow">choice type</a> by passing just an array to show a selectBox with the id and name of my related entity in the template. I always get the same error, asking me to insert an array of entity objects in that choiceS option.</p>
  
  <p>Lets look at some examples at the formType, buildForm function of the main entity:
  <br />
  <br /></p>
  
  <ul>
  <li><strong>WORKS</strong>, default Symfony 2 generated code with null type:</li>
  </ul>
  
  <blockquote>
    <p>->add('relatedEntity', null, array('label'=> 'relatedEntity'))</p>
  </blockquote>
  
  <p><br /></p>
  
  <ul>
  <li><strong>WORKS</strong>, with 'entity' type and a simple queryBuilder:</li>
  </ul>
  
  <blockquote>
  <pre><code>-&gt;add('relatedEntity', 'entity', array(
    'class' =&gt; 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity',
    'query_builder' =&gt; function (EntityRepository $er) {
            $queryBuilder = $er-&gt;createQueryBuilder('relatedEntity');
            $queryBuilder-&gt;resetDQLPart('select');
            $queryBuilder-&gt;resetDQLPart('from');
            $queryBuilder-&gt;select('relatedEntity')
                -&gt;from('ProjectNameRelatedEntityBundle:RelatedEntity', 'relatedEntity');
            return $queryBuilder;
        },
    'property' =&gt; 'descripcion'
  ))
  </code></pre>
  </blockquote>
  
  <p><br /></p>
  
  <ul>
  <li><strong>DOESNT WORK</strong> with 'choice' type, with 'choices' option passing an array of values:</li>
  </ul>
  
  <blockquote>
    <p>$arrayValues = array('1'=>'name1', '2'=>'name2', '3'=>'name3');</p>
  
  <pre><code>    -&gt;add('relatedEntity', 'choice', array(
          'choices' =&gt; $arrayValues,
          'multiple' =&gt; false,
          'label'=&gt; 'relatedEntity'
      ))
  </code></pre>
  </blockquote>
  
  <p><br /></p>
  
  <ul>
  <li><strong>DOESNT WORK</strong> with 'entity' type, with 'choices' option passing an array of values:</li>
  </ul>
  
  <blockquote>
    <p>$arrayValues = array('1'=>'name1', '2'=>'name2', '3'=>'name3');</p>
  
  <pre><code>    -&gt;add('relatedEntity', 'entity', array(
          'class' =&gt; 'ProjectName\RelatedEntityBundle\Entity\RelatedEntity',
          'choices' =&gt; $arrayValues ,
          'multiple' =&gt; false,
          'label'=&gt; 'relatedEntity'
      ))
  </code></pre>
  </blockquote>
  
  <p><br /></p>
  
  <p>I have also tested trying to hack the choices input requeriment by building an array of objets of my related entity, but it asks me to persists those entities before being sent to the choice type.</p>