我已经使用 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() 函数时,出现此错误:
小怪兽爱吃肉