我有实体位和关系ManyToMany开发人员,对于开发人员我使用* @Groups({" for_project"})并且当我使用此anotation时,作为回应没有看到字段开发人员,我看文档并看到* @MaxDepth(2)并使用此但仍然为null。为什么?
{
"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使用时,我们有关系字段的最大深度,例子我有实体Bit,有开发人员和开发人员有用户,如果我想要可见字段实体用户我需要为现场开发人员添加@MaxDept(3)实体位
答案 0 :(得分:2)
当没有@MaxDept使用时,我们有关系字段的最大深度,例子我有实体Bit,有开发人员和开发人员有用户,如果我想要可见字段实体用户我需要为现场开发人员添加@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;
答案 1 :(得分:0)
这适用于我使用fos rest版本:2 *和JMS序列化程序版本:3 *
return $this->view($data)->setContext((new Context())->enableMaxDepth());