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 !

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

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致