1 回答
TA贡献1852条经验 获得超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.yml
services:
#...
TicketSerializationSubscriber:
public: false
arguments: ['@doctrine.orm.entity_manager']
tags:
- { name: kernel.event_subscriber }
#...
确保在进行更改后clear和warmupSymfony 缓存。
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报