You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Symfony中利用postUpdate生命周期回调发送邮件的实现问题

问题:实体更新且验证通过时发送邮件的Doctrine事件订阅者实现

我想在Candidature实体更新且getIsVerified()返回true时发送邮件,打算用Doctrine的事件订阅者,但不清楚具体实现方法。以下是我最初编写的MailEvent.php代码:

class MailEvent
 {
// 实体监听器方法接收两个参数:
// 实体实例和生命周期事件
public function postUpdate(Candidature $candidature, Annonce $annonce, MailerInterface $mailer, LifecycleEventArgs $event): void
{   
$change = $event->getEntityChangeSet();

if ($change instanceof Candidature and $candidature->getIsVerified()) {
    $email = (new Email())
        ->from('trtconseilexample@gmail.com')
        ->to($annonce->getEmail())
        ->subject('您有一位新候选人!')
        ->text("{$candidature->getNom()} {$candidature->getPrenom()} 已申请您的职位,您可以在附件中查看他的简历!")
        ->attachFromPath("public/uploads/images/{$candidature->getCv()}");
    ;
    $mailer->send($email);
}
}

希望有人能提供指导,感谢帮助。


编辑:问题已解决!感谢大家!以下是修正后的MailEvent.php代码:

class MailEvent
{
  // 实体监听器方法接收两个参数:
  // 实体实例和生命周期事件

public function postUpdate(Candidature $candidature, LifecycleEventArgs $event)
{
    $request = $this->requestStack->getCurrentRequest();

     $entityManager = $this->entityManager;

    $annonce = $entityManager->getRepository(Annonce::class)->findOneBy([
       'id' => $request->get('id')
   ]);


    if ($candidature->getIsVerified()) {
    $email = (new Email())
    ->from('trtconseilexample@gmail.com')
    ->to($annonce->getEmail())
    ->subject('您有一位新候选人!')
    ->text("{$candidature->getNom()} {$candidature->getPrenom()} 已申请您的职位,您可以在附件中查看他的简历!")
    ->attachFromPath("public/uploads/images/{$candidature->getCv()}");
;
$this->mailer->send($email);
    }

}

public function __construct(
    private readonly EntityManagerInterface $entityManager,
    private readonly MailerInterface $mailer,
    private readonly RequestStack $requestStack
){

}

内容的提问来源于stack exchange,提问作者Epson184

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 20:18:34