title: How to dynamically retrieve the user who posted the ad to send him a message
body: <p><em>Hello, I am a beginner with symfony 5.3, I am working on a classified ads project, I cannot retrieve the user who posted the ad to send him a message, with my code below I retrieve in the form message to all users registered on the site.</em>
  <strong>The controller:</strong></p>
  <pre><code> #[Route('/users/messages/send/', name: 'send')]
  
      public function send(Request $request): Response
      {
  
          $message = new Messages;
          $form = $this-&gt;createForm(MessagesType::class, $message);
          
          $form-&gt;handleRequest($request);
  
             
          if($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()){
              $message-&gt;setSender($this-&gt;getUser());
  
    $this-&gt;getUser();
   $message-&gt;setRecipient($this-&gt;getUser());
  
              $em = $this-&gt;getDoctrine()-&gt;getManager();
              $em-&gt;persist($message);
              $em-&gt;flush();
  
              $this-&gt;addFlash(&quot;message&quot;, &quot;Message envoyé avec succès.&quot;);
              return $this-&gt;redirectToRoute(&quot;messages&quot;);
          }
  
          return $this-&gt;render(&quot;users/messages/send.html.twig&quot;, [
              &quot;form&quot; =&gt; $form-&gt;createView()
          ]);
      }
    
      #[Route('/users/messages/received/', name: 'received')]
      public function received(): Response
      {
          return $this-&gt;render('users/messages/received.html.twig');
      }
  </code></pre>
  <p><em>I created the form for sending a message with create: form, and I made some modifications.</em>
  <strong>The form type</strong></p>
  <pre><code>class MessagesType extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options): void
      {
         
          $builder
              
              -&gt;add('title', TextType::class, [
                  &quot;attr&quot; =&gt; [
                    &quot;class&quot; =&gt; &quot;form-control&quot;
                  ]
              ])
              -&gt;add('message', TextareaType::class, [
                  &quot;attr&quot; =&gt; [
                     &quot;class&quot; =&gt; &quot;form-control&quot;
                  ]
              ])
              
              -&gt;add('recipient', EntityType::class, [
                  &quot;class&quot; =&gt; Users::class,
                'query_builder' =&gt; function (EntityRepository $em){
                    return $em-&gt;createQueryBuilder('users');
                },
                  &quot;attr&quot; =&gt; [
                     &quot;class&quot; =&gt; &quot;form-control&quot;
                  ], 
              
              ])
              -&gt;add('envoyer', SubmitType::class, [
                  &quot;attr&quot; =&gt; [
                     &quot;class&quot; =&gt; &quot;btn bnt-primary&quot;
                  ]
              ])
          ;
      }
  
      public function configureOptions(OptionsResolver $resolver): void
      {
          $resolver-&gt;setDefaults([
              'data_class' =&gt; Messages::class,
          ]);
      }
  }
  </code></pre>