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

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


/**

 * @param Ticket $object

 *

 * @return string

 */

public function findByTicketDeadline($object)

{

  // some logic

}

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


这是我的类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() 函数时,出现此错误:

http://img3.mukewang.com/60d599960001fafa12740107.jpg

函数式编程
浏览 187回答 1
1回答

小怪兽爱吃肉

您当前的代码失败,因为$ticketRepo = $event->getObject();它返回的Ticket对象是它上面一行中的对象,而不是预期的TicketRepository.您需要将EntityManageror Repository(如果您有存储库作为服务)注入您TicketSerializationSubscriber::__construct()的TicketRepository.假设您已autowire启用包含TicketSerializationSubscriber.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.ymlservices:    #...    TicketSerializationSubscriber:        public: false        arguments: ['@doctrine.orm.entity_manager']        tags:            - { name: kernel.event_subscriber }            #...确保在进行更改后clear和warmupSymfony 缓存。
打开App,查看更多内容
随时随地看视频慕课网APP