Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. <?php
  2. namespace AppBundle\Form;
  3.  
  4. class FormType extends AbstractType
  5. {
  6.    
  7.     /** @var UserRepository */
  8.     private $userRepository;
  9.  
  10.     /** @var ApiKeyRepository */
  11.     private $apiKeyRepository;
  12.  
  13.     public function __construct(UserRepository $userRepository)
  14.     {
  15.         $this->userRepository = $userRepository;
  16.     }
  17.  
  18.     public function buildForm(FormBuilderInterface $builder, array $options): void
  19.     {
  20.         $builder
  21.             ->add(
  22.                 self::APPLICANT,
  23.                 ChoiceType::class,
  24.                 [
  25.                     'required' => false,
  26.                     'label' => 'Richiesto da...',
  27.                     'choices' => $this->getLegalUsers(),
  28.                     'attr' => [
  29.                         'data-helper' => 'chosen',
  30.                     ],
  31.                 ]
  32.             )
  33.     }
  34.  
  35.     private function getLegalUsers(): array
  36.     {
  37.         $choices = [];
  38.  
  39.         $query = // qui fai la query con la distinct
  40.  
  41.         /** @var User[] $users */
  42.         $users = $this->userRepository->findByQuery($query);
  43.  
  44.         foreach ($users as $user) {
  45.             $choices[$user->getFirstName() . ' ' . $user->getLastName()] = $user->getEmail();
  46.         }
  47.  
  48.         return $choices;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement