我有实体位和关系 ManyToMany 开发人员,对于开发人员,我使用 * @Groups({"for_project"}) ,当我使用此注释时,作为响应,看不到字段开发人员,我查看文档并看到 * @MaxDepth(2 )并使用它但仍然为空。为什么 ?
{
"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"))
);
已解决
我明白,需要为字段实体开发人员添加 * @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;
关于php - Symfony Rest Controller 限制序列化深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959197/