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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置