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')));
}
}
}