title: Symfony2 form queryBuilder find distinct object
body: <p>I have an Organization entity. </p>
  
  <pre><code>Acme\BaseBundle\Entity\Organization:
  type: entity
  ...
  oneToMany:
      childOrganizations:
          targetEntity: Organization
          mappedBy: parentOrganization
  manyToOne:
      parentOrganization:
          targetEntity: Organization
          inversedBy: childOrganizations
          nullable: true
  </code></pre>
  
  <p>I have this form:</p>
  
  <pre><code>-&gt;add('parentOrganization', 'entity', array(
      'class'         =&gt; "AcmeBaseBundle:Organization",
      'property'      =&gt; 'name',
      'required'      =&gt; false,
      'query_builder' =&gt; function(EntityRepository $er)
          {
             return $er
               -&gt;createQueryBuilder('o')
               -&gt;select('o')
               -&gt;add('groupBy', 'o.name');
          },
  ))
  </code></pre>
  
  <p>Then I would like to show only different organizations. I mean if I have first, second and third organization. Then I have a form for the first organization and I would like to have just two choices in parentOrganization entity field (second and third), but in this case I have three choices (first can be parent of first).</p>
  
  <p>Is there some way to leave out possibility to connect with own org?</p>
  
  <p>EDIT: The problem is that I am working in form class and I don't know how to handle Organization object here. This is my code:</p>
  
  <pre><code>class OrganizationType extends AbstractType
  {
          /**
       * @param FormBuilderInterface $builder
       * @param array $options
       */
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
              -&gt;add('name', null, array('max_length' =&gt; 100))
              -&gt;add('type')
              -&gt;add('parentOrganization', 'entity', array(
                      'class'         =&gt; "AcmeBaseBundle:Organization",
                      'property'      =&gt; 'name',
                      'required'      =&gt; false,
                      'query_builder' =&gt; function(EntityRepository $er)
                          {
                              return $er-&gt;createQueryBuilder('o')
                                  -&gt;select('o')
                                  -&gt;add('groupBy', 'o.name');
                          }
                  ))
              -&gt;add('addresses', 'bootstrap_collection', array(
                  'type'                  =&gt; new AddressType(),
                  'allow_add'             =&gt; true,
                  'allow_delete'          =&gt; true,
                  'add_button_text'       =&gt; '+',
                  'delete_button_text'    =&gt; '-',
                  'by_reference'          =&gt; false,
                  'sub_widget_col'        =&gt; 9,
                  'button_col'            =&gt; 3
              ))
      }
      /**
       * @param OptionsResolverInterface $resolver
       */
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
           $resolver-&gt;setDefaults(array(
              'data_class' =&gt; 'Acme\BaseBundle\Entity\Organization'
          ));
      }
  
      public function getName()
      {
          return 'acme_basebundle_organization';
      }
  }
  </code></pre>