在Symfony中,如何在EventPriorities :: POST_WRITE之后保存对象

时间:2018-12-16 15:19:13

标签: php symfony events

我有事件POST_WRITE的订阅者。通常,在这种方法中,我可以获得保存的对象。我该怎么办?

final class ProductCreateSubscriber implements EventSubscriberInterface
{
    private $entityManager;
    private $hostRepository;
    private $jwtEncoder;
    private $tokenExtractor;

    public function __construct(EntityManagerInterface $entityManager, HostRepository $hostRepository, JWTEncoderInterface $jwtEncoder, TokenExtractorInterface $tokenExtractor)
    {
        $this->jwtEncoder = $jwtEncoder;
        $this->entityManager = $entityManager;
        $this->hostRepository = $hostRepository;
        $this->tokenExtractor = $tokenExtractor;
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::VIEW => ['createProductWatcher', EventPriorities::POST_WRITE],
        ];
    }

    public function createProductWatcher(GetResponseForControllerResultEvent $event)
    {
        ---------- HERE I NEED SAVED OBJECT -----------
        $token = $this->tokenExtractor->extract($event->getRequest());
        if ($token) {
            $payload = $this->jwtEncoder->decode($token);
            $productWatcher = new ProductWatcher();
        }
    }
}

我可以通过这种方法获取对象吗?还是需要使用EventListeners?

0 个答案:

没有答案