我从angular side向symfony发送了一个gethttp请求以获取命令详细信息,我有许多嵌套的json对象和循环引用,我正在尝试将MaxDepth设置为相关实体,正如jms的文档所说,添加此行
$serializer->serialize($command, 'json', SerializationContext::create()->enableMaxDepthChecks());
创建序列化程序上下文并启用maxDepth属性
这是我的实体
<?php
namespace AppEntity;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use JMSSerializerAnnotationMaxDepth;
/**
* @ORMEntity(repositoryClass="AppRepositoryCommandRepository")
*/
class Command
{
/**
* @ORMId()
* @ORMColumn(type="string", length=255)
*/
private $id;
/**
* @ORMColumn(type="float")
*/
private $price;
/**
* @ORMColumn(type="text")
*/
private $adresse;
/**
* @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="commands")
* @ORMJoinColumn(nullable=true, onDelete="CASCADE")
*/
private $user;
/**
* @ORMOneToMany(targetEntity="AppEntityCommandLine", mappedBy="command")
*/
private $commandLines;
/**
* @ORMOneToMany(targetEntity="AppEntityGiftCheck", mappedBy="command")
* @MaxDepth(1)
*/
private $giftChecks;
命令详细信息的方法
/**
* Get Commands.
* @RestGet("/CommandDetails/{id}")
* @param Request $request
* @return View
*/
public function CommandDetails( Request $request,$id)
{
$entityManager = $this->getDoctrine()->getManager();
$command = $this->getDoctrine()->getRepository(Command::class)->find($id);
$command-> setUser($this->getUserDetails( $command-> getUser() ) );
$serializer->serialize($command, 'json', SerializationContext::create()->enableMaxDepthChecks());
return $this->handleView($this->view($command));
}
服务器给出500错误似乎很正常,因为不知道$serializer变量,因为它没有初始化,我没有找到如何从什么接口初始化这个变量!请帮忙?
跟随链路
JMSSerializerBundle,您使用它是一项服务,您可以直接在控制器方法中自动连接它:
public function CommandDetails(Request $request, SerializerInterface $serializer, $id)
但首先添加到service.yaml文件的下一行:
services:
JMSSerializerSerializerInterface: '@jms_serializer'