使用 EventSubscriber 将函数的返回值作为 属性 传递给 json

Pass the returned value of a function as a property to json using EventSubscriber

我使用 JMS 序列化包构建了一个 api。此外,我有一个实体 Ticket,它有一个名为 TicketRepository 的存储库。此存储库包含以下方法:

/**
 * @param Ticket $object
 *
 * @return string
 */
public function findByTicketDeadline($object)
{
  // some logic
}

我的目标:使用EventSubscriber将这个函数的返回值传递给json。

这是我的 class TicketSerializationSubscriber

 class TicketSerializationSubscriber implements EventSubscriberInterface
{


    /**
     * @param ObjectEvent $event
     */
    public function onPostSerialize(ObjectEvent $event)
    {
        /** @var JsonSerializationVisitor $visitor */
        $visitor = $event->getVisitor();
        $visitor->setData('ticketDeadline', 'TEST TICKET'); // WORKS

        /** @var Ticket $ticket */
        $ticket = $event->getObject();
        /** @var TicketRepository $ticketRepo */
        $ticketRepo = $event->getObject();

        $visitor->setData('TESTING', $ticketRepo->findByTicketDeadline($ticket));

//        $visitor->setData('uri',
//                $this->router->generate('ticket_api_show', [
//                    'ticketDeadline' => $ticketRepo->findByTicketDeadline($ticket)
//                ])
//        );

    }

    public static function getSubscribedEvents()
    {
        return array(
            array(
                'event' => 'serializer.post_serialize',
                'method' => 'onPostSerialize',
                'class' => 'CMS3\CoreBundle\Entity\Ticket', // if no class, subscribe to every serialization
                //'class' => 'CMS3\CoreBundle\Repository\TicketRepository', // if no class, subscribe to every serialization
                'format' => 'json', // optional format
                //'priority' => 0, // optional priority
            ),
        );
    }

}

当我将存储库方法传递给 $visitor->setData() 函数时,出现此错误:

您当前的代码失败,因为 $ticketRepo = $event->getObject(); 正在返回上面一行中的 Ticket 对象,而不是预期的 TicketRepository

您需要将 EntityManagerRepository(如果您有存储库作为服务)注入到您的 TicketSerializationSubscriber::__construct() 中以检索 TicketRepository

假设您为包含 TicketSerializationSubscriber.

的目录启用了 autowire
use Doctrine\ORM\EntityManagerInterface;

class TicketSerializationSubscriber implements EventSubscriberInterface
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
         $this->em = $em;
    }

    public function onPostSerialize(ObjectEvent $event)
    {
        /** @var JsonSerializationVisitor $visitor */
        $visitor = $event->getVisitor();

        $ticket = $event->getObject();
        if (!$ticket instanceof Ticket) {
            //ensure object is of expected type
            return;
        }

        /** @var TicketRepository $ticketRepo */
        $ticketRepo = $this->em->getRepository(Ticket::class);

        $visitor->setData('TESTING', $ticketRepo->findByTicketDeadline($ticket));
        //...
    }

    //...

}

如果不使用 autowire,您将需要手动将其添加到您的 TicketSerializationSubscriber 服务声明参数中。

#app/config/services.yml

services:
    #...

    TicketSerializationSubscriber:
        public: false
        arguments: ['@doctrine.orm.entity_manager']
        tags:
            - { name: kernel.event_subscriber }
            #...

确保 clearwarmup Symfony 缓存在进行更改后。