php - Symfony 让用户了解多对多的关系
问题描述
在多对多的关系中
在控制器中,如何让所有用户加入特定事件?
我试图从特定事件中获取用户,但它会获取所有事件的所有用户。
我需要从特定事件中获取用户,因为我想通过电子邮件通知所有这些用户。
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(),
]);
}
解决方案
这与https://stackoverflow.com/a/65905944/6127393几乎完全相同
$users = $evento->GetUsers();
...
if ($form->isSubmitted() && $form->isValid()) {
foreach($users as $user){
$email = $user->getEmail();
...
}
}
推荐阅读
- c - 为什么有些数字印有小数位?
- ethereum - Solidity 文档中的简单公开拍卖示例合同不支付受益人
- python - 继承类的 __init__ 调用内部函数的类
- google-photos - 尝试获取 oauth2 访问令牌时出现“invalid_scope”
- visual-studio-code - Cpplint 找不到可执行文件
- javascript - cb=gapi.loaded_0:76 未捕获的 uO {消息:“无效的 cookiePolicy”
- python - 如何在 Python 的字符串列表中使用“.endswith”?
- html - 停止在桌面上制作大于原始尺寸的图像
- asp.net-core-signalr - 什么是默认的 Javascript SignalR 客户端身份验证方案?
- python - 如何在不和谐[Python]中制作随机输出命令