Symfony form query builder

<?php

namespace App\AccessBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Doctrine\ORM\EntityRepository;
use App\AccessBundle\Entity\Application;
use App\AccessBundle\Entity\Access;
use App\UserBundle\Entity\User;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;

class AccessType extends AbstractType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $departmentId = $options['departmentId'];

        $builder
                ->add('application', EntityType::class, ['class' => Application::class, 'choice_label' => 'name', 'required' => true])
                ->add('user', EntityType::class, ['class' => User::class, 'query_builder' => function (EntityRepository $er) use ($departmentId) {
                        return $er->createQueryBuilder('i')
                                ->where('i.department = :Department')->setParameter('Department', $departmentId)
                                ->orderBy('i.name', 'ASC')
                                ->addOrderBy('i.surname', 'ASC');
                    }, 'choice_label' => function (User $user) {
                        return $user->getName() . ' ' . $user->getSurname();
                    },
                    'multiple' => false, 'required' => true])
                ->add('description', TextareaType::class, ['required' => false, 'attr' => ['maxlength' => 255]])
                ->add('datetime_add', DateTimeType::class, ['required' => true, 'widget' => 'single_text'])
                ->add('datetime_block', DateTimeType::class, ['required' => false, 'widget' => 'single_text'])
                ->add('datetime_remove', DateTimeType::class, ['required' => false, 'widget' => 'single_text'])
                ->add('login', TextType::class, ['required' => true, 'attr' => ['maxlength' => 255]])
                ->add('submit', SubmitType::class)
                ->add('submitAndClose', SubmitType::class)
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'data_class' => Access::class,
            'departmentId' => null,
        ]);
    }

}
Komentarze wyłączone