我从有角度的一侧向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发送一个get http请求以获取命令详细信息,我有许多嵌套的json对象和圆形引用,我正在尝试将MaxDepth设置为相关实体,并作为...