dongtan1009 2017-05-11 20:48
浏览 72
已采纳

使用实体作为symfony中EntityType输入过滤器的参数

What I want to do is use the Entity passed to the createForm method in my controller to filter an EntityType field.

Entities are related:

Employee('1')----('1')User('M')-----('1')TicketQueue('M')----('1')Ticket

user('M')----('1')ticket

what I want to do is use the ticket relation to ticketqueue, in order to be able to filter the user field to only users that have an employee profile and are assigned to the queue which the ticket is assigned to.

so far this is where I am:

I'm sucessfully filling some of my conditions which are: -only users with employee profile -only users with an assigned queue

but I haven't gotten to how to filter out with a where statement where TicketQueue = "whatever the ticket is assigned to"

now this form will only be used while editing the ticket status and assignee(user).

Form class:

class TicketUpdateType extends AbstractType {

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('assignedto', EntityType::class, array('multiple' => false,
                'class' => 'AuthBundle\Entity\User', 'placeholder' => 'Select Personel', 
                    'query_builder' => function (UserRepository $er) {
                        return $er->createQueryBuilder('u')
                        ->select('u') 
                        ->join('u.employee','e')
                        ->join('u.ticketQueues','tq')                                  
                        ->orderBy('u.username', 'ASC');                                                  
                    },
                'label' => "Assigned To:",
                'choice_label' => function
                ($q) {
                    return $q->getEmployee()->getFirstName()." ".$q->getEmployee()->getLastName();
                }, 'attr' => array('class' => 'form-control')))                   
            ->add('ticketstatus', EntityType::class, array('multiple' => false,
                'class' => 'TicketBundle\Entity\TicketStatus', 'placeholder' => 'Select Status','label' => "Ticket Status", 'choice_label' => function
                ($q) {
                    return $q->getName();
                }, 'attr' => array('class' => 'form-control')));               
}

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => Ticket::class,
    ));
}
}

EDIT:

Form:

class TicketUpdateType extends AbstractType{

public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder

            ->addEventSubscriber(new AddQueueFieldSubscriber())                

            ->add('ticketstatus', EntityType::class, array('multiple' => false,
                'class' => 'TicketBundle\Entity\TicketStatus', 'placeholder' => 'Select Status','label' => "Ticket Status", 'choice_label' => function
                ($q) {
                    return $q->getName();
                }, 'attr' => array('class' => 'form-control')));               
}


public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
        'data_class' => Ticket::class,
    ));
}
}

Event subscriber:

class AddQueueFieldSubscriber implements EventSubscriberInterface{

public static function getSubscribedEvents()
{
    // Tells the dispatcher that you want to listen on the form.pre_set_data
    // event and that the preSetData method should be called.
    return array(FormEvents::PRE_SET_DATA => 'preSetData');
}


public function preSetData(FormEvent $event)
{
    $ticket = $event->getData();
    $form = $event->getForm();
    $queue = $event->getData()->getTicketQueue()->getName();

    if ($ticket) {
        $form->add('assignedto', EntityType::class, array('multiple' => false,
                'class' => 'AuthBundle\Entity\User', 'placeholder' => 'Select Personel', 
                    'query_builder' => function (UserRepository $er) use ($queue){
                        return $er->createQueryBuilder('u')
                        ->select('u') 
                        ->join('u.employee','e')
                        ->join('u.ticketQueues','tq')   
                        ->where('tq.name = :queue')        
                        ->orderBy('u.username', 'ASC')
                        ->setParameter('queue', $queue);        
                    },
                'label' => "Assigned To:",
                'choice_label' => function
                ($q) {
                    return $q->getEmployee()->getFirstName()." ".$q->getEmployee()->getLastName();
                }, 'attr' => array('class' => 'form-control')));  
    }
}    
}
  • 写回答

1条回答 默认 最新

  • dongyu1125 2017-05-12 08:53
    关注

    "What I want to do is use the Entity passed to the createForm method in my controller to filter an EntityType field."

    You need to use a PRE_SET_DATA function in your form : symfony doc

    This function allow you to prepare your form.

    exemple :

    $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $form = $event->getForm();
                $entity = $event->getData();
    
                if (!$entity->getEnabled()) {
                    $form->remove('validity');
                }
            }
        );
    

    Don't forget to init your form with the entity in your controller :

    $form = $this->createForm(TicketUpdateType::class, $yourEntity);

    Good luck !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线