我正在嘗試在物體更新(和 getVerified)時發送電子郵件,我使用了來自學說的事件訂閱者,但我不知道該怎么做。
我的 MailEvent.php :
class MailEvent
{
// the entity listener methods receive two arguments:
// the entity instance and the lifecycle event
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('[email protected]')
->to($annonce->getEmail())
->subject('Vous avez un nouveau candidat !')
->text("{$candidature->getNom()} {$candidature->getPrenom()} a postulé à votre annonce, vous trouverez son CV en pièce jointe !")
->attachFromPath("public/uploads/images/{$candidature->getCv()}");
;
$mailer->send($email);
}
}
如果有人能幫助我理解,謝謝。
編輯:問題解決!謝謝你們 !郵件事件.php:
class MailEvent
{
// the entity listener methods receive two arguments:
// the entity instance and the lifecycle event
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('[email protected]')
->to($annonce->getEmail())
->subject('Vous avez un nouveau candidat !')
->text("{$candidature->getNom()} {$candidature->getPrenom()} a postulé à votre annonce, vous trouverez son CV en pièce jointe !")
->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
){
}
}
uj5u.com熱心網友回復:
您需要確保在您的services.yaml
App\EventListener\MailEvent:
tags:
- name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'App\Entity\Candidature'
然后你需要在你的監聽器中使用 postUpdate 函式,
public function postUpdate(Candidature $candidature, LifecycleEventArgs $event): void
{
...
}
它只能接受 2 個引數,如果您需要訪問服務,則需要在建構式中使用依賴注入:
public function __construct(
private readonly AnnonceRepository $annonceRepository,
private readonly MailerInterface $mailer,
) {
}
然后你可以在你的 postUpdate 函式中使用這些,我不知道你是如何Annonce在你的專案中獲得相關的,但你可以使用存盤庫去從資料庫中獲取它。
public function postUpdate(Candidature $candidature, LifecycleEventArgs $event): void
{
//maybe ?
$annonce = $this->annonceRepository->findOneBy["candidature" => $candidature];
//This has to be a Candidature entity, as defined in the services.yaml, so no need to check that, you also do not use the changeset at all so no need for that either
if ($candidature->getIsVerified()) {
$email = (new Email())
->from('[email protected]')
->to($annonce->getEmail())
->subject('Vous avez un nouveau candidat !')
->text("{$candidature->getNom()} {$candidature->getPrenom()} a postulé à votre annonce, vous trouverez son CV en pièce jointe !")
->attachFromPath("public/uploads/images/{$candidature->getCv()}");
;
$this->mailer->send($email);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/503887.html
