Php Symfony Rest控制器限制序列化深度
我有许多开发者的实体位和关系,对于开发者,我使用*@Groups({“for_project”}),当我使用这个选项时,作为响应,我没有看到字段开发者,我查看文档,看到*@MaxDepth(2)并使用它,但仍然有null。为什么?Php Symfony Rest控制器限制序列化深度,php,symfony,fosrestbundle,jmsserializerbundle,Php,Symfony,Fosrestbundle,Jmsserializerbundle,我有许多开发者的实体位和关系,对于开发者,我使用*@Groups({“for_project”}),当我使用这个选项时,作为响应,我没有看到字段开发者,我查看文档,看到*@MaxDepth(2)并使用它,但仍然有null。为什么? { "0": { "id": 501, "created": "2015-11-27T12:25:11+0200", "developer_id": {}, "rate": 4, "comment": "fsefsf" }, "1": { "id": 502, "c
{
"0": {
"id": 501,
"created": "2015-11-27T12:25:11+0200",
"developer_id": {},
"rate": 4,
"comment": "fsefsf"
},
"1": {
"id": 502,
"created": "2015-11-27T12:25:46+0200",
"developer_id": {},
"rate": 3,
"comment": "feasf"
}
}
class Bit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose()
* @Groups({"for_project"})
*/
private $id;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @Expose()
* @Groups({"for_project"})
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
* @Groups({"for_project"})
*/
private $updated;
/**
* @var \Artel\ProfileBundle\Entity\Developer
*
* @ORM\ManyToOne(targetEntity="Developer")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="developer_id", referencedColumnName="id", onDelete="CASCADE")
* })
* @Expose()
* @Groups({"for_project"})
* @MaxDepth(2)
*/
private $developerId;
class BitController extends FOSRestController
...anotaion block
public function getBitByProjectAction($id, $token)
{
$this->getDoctrine()->getRepository('ArtelProfileBundle:Users')->findOneBySecuritytoken($token);
if (!empty($user) || $security->isGranted('ROLE_ADMIN') ) {
$bits = $this->getDoctrine()->getManager()
->getRepository('ArtelProfileBundle:Bit')
->findBitByProject($id, $token);
if (!$bits) {
throw new NotFoundHttpException();
}
return View::create()
->setStatusCode(200)
->setData($bits)
->setSerializationContext(
SerializationContext::create()
->enableMaxDepthChecks()
->setGroups(array("for_project"))
);
已解决我明白,需要为fields实体开发者添加*@Groups({“for_project”}) 但当我删除@MaxDepth时,我还有字段实体开发者,为什么需要@MaxDepth?
我理解,当不使用@MaxDept时,我们有关系字段的最大深度,例如我有实体位,位有开发者,开发者有用户,如果我想要可见字段实体用户,我需要在实体位中为字段开发者添加@MaxDept(3)当不使用@MaxDept时,我们有关系字段的最大深度,示例我有实体位,位有开发者,开发者有用户,如果我想要可见字段实体用户,我需要在实体位中为字段开发者添加@MaxDept(3) 在行动中:
return View::create()
->setStatusCode(200)
->setData($bits)
->setSerializationContext(
SerializationContext::create()
->enableMaxDepthChecks()
->setGroups(array("for_project"))
);
作为回应
[
{
"id": 501,
"created": "2015-11-30T17:49:19+0200",
"developer_id": {
"id": 201,
"rate": 0,
"user": [
{
"first_name": "Ivan",
"last_name": "Shuba"
}
]
},
"rate": 4,
"comment": "fsefse"
}
]
和实体
class Bit
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @Expose()
* @Groups({"for_project"})
*/
private $id;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
* @Expose()
* @Groups({"for_project"})
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
* @Groups({"for_project"})
*/
private $updated;
/**
* @var \Artel\ProfileBundle\Entity\Developer
*
* @ORM\ManyToOne(targetEntity="Developer")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="developer_id", referencedColumnName="id", onDelete="CASCADE")
* })
* @Expose()
* @Groups({"for_project"})
* @MaxDepth(3)
*/
private $developerId;
class Developer
{
/**
* @ORM\OneToMany(targetEntity="Artel\ProfileBundle\Entity\Users", mappedBy="developer", cascade={"persist", "remove"})
* @Expose()
* @Groups({"for_project"})
*/
protected $user;
class Users implements UserInterface
{
/**
* @var string
*
* @ORM\Column(name="first_name", type="string", length=255, nullable=false)
* @Expose()
* @Assert\Length(min=3, max=255)
* @Groups({"for_project"})
*/
protected $firstName;
/**
* @var string
*
* @ORM\Column(name="last_name", type="string", length=255, nullable=true)
* @Expose()
* @Groups({"for_project"})
*/
protected $lastName;
这适用于我使用fos rest版本:2*和JMS序列化程序版本:3*
return $this->view($data)->setContext((new Context())->enableMaxDepth());
删除@MaxDepth注释时得到了什么?另外,你们能不能显示导入的类?当我删除@MaxDepth时,我还有空的开发者,当我删除*@Groups({“for_project”})时,我有关于开发者的信息。我需要为fields实体开发者添加*@Groups({“for_project”})?我知道我需要为文件添加*@Groups({“for_project”})developer@shuba.ivan将您的解决方案作为答案发布;)很高兴知道你解决了