【发布时间】:2020-02-21 13:43:58
【问题描述】:
我从 Angular 向 symfony 发送一个 get http 请求以获取命令详细信息,我有许多嵌套的 json 对象和循环参考,我正在尝试将 MaxDepth 设置为相关实体,并且正如 jms 的文档所说,添加这一行
$serializer->serialize($command, 'json', SerializationContext::create()->enableMaxDepthChecks());
创建序列化程序上下文并启用 maxDepth 属性
这是我的实体
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\MaxDepth;
/**
* @ORM\Entity(repositoryClass="App\Repository\CommandRepository")
*/
class Command
{
/**
* @ORM\Id()
* @ORM\Column(type="string", length=255)
*/
private $id;
/**
* @ORM\Column(type="float")
*/
private $price;
/**
* @ORM\Column(type="text")
*/
private $adresse;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="commands")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
*/
private $user;
/**
* @ORM\OneToMany(targetEntity="App\Entity\CommandLine", mappedBy="command")
*/
private $commandLines;
/**
* @ORM\OneToMany(targetEntity="App\Entity\GiftCheck", mappedBy="command")
* @MaxDepth(1)
*/
private $giftChecks;
命令方法详解
/**
* Get Commands.
* @Rest\Get("/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变量导致它没有初始化,我没有找到如何从什么接口初始化这个变量!请帮忙?
已关注link
【问题讨论】:
标签: symfony serialization nested jms jmsserializerbundle