函数getDoctrine不适用于控制器的一项特定功能

时间:2019-02-21 22:20:51

标签: php symfony controller doctrine

编辑:现在尝试将EventSubscriber用作b.enoit.be建议我。但是当我发送邮件时,什么也没发生。我没有收到任何错误,但是邮件也没有存储在我的数据库中。我测试了我做错了什么?

SentMailsListener.php:

SELECT RPin, Variety, SubDivisionId, BlockId,
       STUFF( (SELECT ',' + CAST(re2.ClearanceID as varchar(255))
               FROM RPin_ExclusionT re2
               WHERE re2.RPin = re.RPin AND
                     (re2.Variety = re.Variety OR re2.Variety IS NULL and re.Variety IS NULL) AND
                     (re2.SubDivisionId = re.SubDivisionId OR re2.SubDivisionId IS NULL AND re.SubDivisionId IS NULL) AND
                     (re2.BlockId = re.BlockId OR re2.BlockId IS NULL AND re.BlockId IS NULL)
               ORDER BY re2.ClearanceID 
               FOR XML PATH('')
              ), 1, 1, '') AS Clearances
FROM (SELECT DISTINCT RPin, Variety, SubDivisionId, BlockId
      FROM RPin_ExclusionR re
     ) re;

services.yml:

<?php

namespace Fidelise\SignUpBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Fidelise\SignUpBundle\Entity\EmailsHistory;
use Swift_Events_SendEvent;

class SentMailsListener implements EventSubscriberInterface
{

    public static function getSubscribedEvents()
    {
        return [Swift_Events_SendEvent::RESULT_SUCCESS => 'onMailSent'];
    }

    public function onMailSent(Swift_Events_SendEvent $event)
    {
        $em = $this->getDoctrine()->getManager();
        $message = $event->getMessage();
        $email = new EmailsHistory();
        $email->setRecipient(key($message->getTo()));
        $email->setSubject($message->getSubject());
        $email->setBody($message->getBody());
        $email->setSender(key($message->getFrom()));
        $em->persist($email);
        $em->flush();
    }
}

1 个答案:

答案 0 :(得分:1)

确保驱动程序从AbstractController扩展。无论如何,没有更多的代码,很难给您一个具体的答案。