首页 > 解决方案 > Symfony 让用户了解多对多的关系

问题描述

在多对多的关系中

在控制器中,如何让所有用户加入特定事件?

我试图从特定事件中获取用户,但它会获取所有事件的所有用户。

我需要从特定事件中获取用户,因为我想通过电子邮件通知所有这些用户。

捕获表 sql

public function notificarATodos(MailerInterface $mailer, Request $request, UserPasswordEncoderInterface $passwordEncoder, Evento $evento, User $user): Response
   {
    $user_repo = $this->getDoctrine()->getRepository(User::class);
    $user = $user_repo->findAll();    
    $evento = $this->getDoctrine()->getRepository(Evento::class)->findOneById($evento);

       //$user->GetEventos($evento);

        $evento->GetUsers($user);       
        dump($user);die;

      $form = $this->createForm(ContactoFormType::class);
      $form->handleRequest($request);
      if ($form->isSubmitted() && $form->isValid()) {

        $email = $user->getEmail();
       $contactFormData = $form->getData();
          $email = (new Email())
          ->from('')
          ->to($user->getEmail())
          ->subject($contactFormData['asunto'],
          'text/plain')
          ->text($contactFormData['mensaje'],
          'text/plain');
      $mailer->send($email);
          $this->addFlash('success', 'EMAIL ENVIADO CORRECTAMENTE');
          return $this->redirect($this->generateUrl('evento_detalle', ['id' => $evento->getId()]));                        
      }
      return $this->render('user/contactar.html.twig', [
          'form' => $form->createView(),
      ]);

  } 

标签: phpsymfony

解决方案


这与https://stackoverflow.com/a/65905944/6127393几乎完全相同

$users = $evento->GetUsers();
...
if ($form->isSubmitted() && $form->isValid()) {
  foreach($users as $user){
    $email = $user->getEmail();
    ...
  }
}

推荐阅读