我想要做的是使用传递给我的控制器中的createForm方法的实体来过滤EntityType字段。
实体是相关的:
员工( '1')----( '1')用户( 'M') - - - - ( '1')TicketQueue( 'M')----( '1')票务
用户( 'M')----( '1')票
我想要做的是使用票证关系到票证队列,以便能够将用户字段过滤到只有具有员工档案并被分配到票证所分配到的队列的用户。
到目前为止,这就是我所在的地方:我成功地填补了我的一些条件: - 仅具有员工档案的用户 - 只有具有指定队列的用户
但我还没有考虑如何过滤掉一个where语句,其中TicketQueue =“无论票证分配给哪个”
现在只有在编辑故障单状态和受让人(用户)时才会使用此表单。
表格类:
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,
));
}
}
修改
形式:
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,
));
}
}
活动订阅者:
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')));
}
}
}
答案 0 :(得分:1)
“我想要做的是使用传递给我的控制器中的createForm方法的实体来过滤EntityType字段。”
您需要在表单中使用PRE_SET_DATA函数: symfony doc
此功能可让您准备表格。
例如:
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) {
$form = $event->getForm();
$entity = $event->getData();
if (!$entity->getEnabled()) {
$form->remove('validity');
}
}
);
不要忘记使用控制器中的实体初始化表单:
$form = $this->createForm(TicketUpdateType::class, $yourEntity);