title: Separation Of Concerns with Symfony Forms
body: <p>I've been trying to use the Symfony 2 forms lately, which are quite good for simple forms.</p>
  
  <p>But -for use in select boxes or something similar- I often want a list of associated entities in the form. In several blogposts and Symfony docs, they suggest something like this...</p>
  
  <pre><code>//BlogPostType implements FormTypeInterface
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
    -&gt;add('category', null, array(
      'property' =&gt; 'name',
      'query_builder' =&gt; function(EntityRepository $er) use($options) {
        return $er-&gt;createQueryBuilder('category')-&gt;orderBy('category.name', 'ASC');
      }
    );
  }
  </code></pre>
  
  <p>As I am paying a lot of attention to domain-driven-design and especially separation of concerns, I find it hard to believe that the only option to bind associated entities to a custom form type in Symfony, is by querying for it in the custom form type.</p>
  
  <p>In my opinion, this violates SoC, because forms should not be querying.
  This way, the form always takes the same entities, but it's not the form who should chose which entities are displayed...</p>
  
  <p>The controller, who asks the form builder to build a form, should inject the associated objects into the custom form type constructor or so...</p>
  
  <pre><code>//BlogPostType implements FormTypeInterface
  public function __construct(array $categories) {
    $this-&gt;categories = $categories;
  }
  
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
      -&gt;add('category', null, array(
        'property' =&gt; 'name',
        'choices' =&gt; $this-&gt;categories
      );
  }
  </code></pre>
  
  <p>Does anyone know how this can be achieved?</p>