Tomsgu
Tomsgu

Reputation: 1016

Symfony2 form queryBuilder find distinct object

I have an Organization entity.

Acme\BaseBundle\Entity\Organization:
type: entity
...
oneToMany:
    childOrganizations:
        targetEntity: Organization
        mappedBy: parentOrganization
manyToOne:
    parentOrganization:
        targetEntity: Organization
        inversedBy: childOrganizations
        nullable: true

I have this form:

->add('parentOrganization', 'entity', array(
    'class'         => "AcmeBaseBundle:Organization",
    'property'      => 'name',
    'required'      => false,
    'query_builder' => function(EntityRepository $er)
        {
           return $er
             ->createQueryBuilder('o')
             ->select('o')
             ->add('groupBy', 'o.name');
        },
))

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).

Is there some way to leave out possibility to connect with own org?

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:

class OrganizationType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', null, array('max_length' => 100))
            ->add('type')
            ->add('parentOrganization', 'entity', array(
                    'class'         => "AcmeBaseBundle:Organization",
                    'property'      => 'name',
                    'required'      => false,
                    'query_builder' => function(EntityRepository $er)
                        {
                            return $er->createQueryBuilder('o')
                                ->select('o')
                                ->add('groupBy', 'o.name');
                        }
                ))
            ->add('addresses', 'bootstrap_collection', array(
                'type'                  => new AddressType(),
                'allow_add'             => true,
                'allow_delete'          => true,
                'add_button_text'       => '+',
                'delete_button_text'    => '-',
                'by_reference'          => false,
                'sub_widget_col'        => 9,
                'button_col'            => 3
            ))
    }
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
         $resolver->setDefaults(array(
            'data_class' => 'Acme\BaseBundle\Entity\Organization'
        ));
    }

    public function getName()
    {
        return 'acme_basebundle_organization';
    }
}

Upvotes: 0

Views: 1928

Answers (1)

aimar
aimar

Reputation: 66

If you create form as written in doc (through method createForm of controller), then your current entity available in $options['data']. And your code will look like:

$entity = $options['data'];
...
'query_builder' => function(EntityRepository $er) use ($entity)
{
    return $er->createQueryBuilder('o')
        ->select('o')
        ->add('groupBy', 'o.name')
        ->add('where', 'o.id != ?1')
        ->setParameter(1, $entity->getId());
}

Upvotes: 1

Related Questions