我使用的是FOSRestBundle --带有自动旋转和自动视图。我在Controller中的操作如下所示:
public function getAction($user_id)
{
$user = $this->em->getRepository('SBGUserBundle:User')->find($user_id);
return $user;
}一切正常,我的JSON格式的响应如下所示:
{
"id": 20,
"username": "fwojciechowski",
"mpks": [{
"id": 91,
"name": "Testowe MPK 1",
"managers": []
}, {
"id": 92,
"name": "Testowe MPK 2",
"teta_id": 1,
"managers": []
}]
}但是我还需要一个级别的深度--我需要"mpks“数组中的”manager“。但在其他情况下,我不需要3个级别。我该怎么做呢?
发布于 2017-02-15 21:36:44
注解
如果你正在使用注解,你可以像下面的yml配置那样做,以供以后参考。首先,您转到manager实体并添加以下内容:
/**
* Manager
*
* @ORM\Table(name="manager")
* @ORM\Entity
*
* @Serializer\ExclusionPolicy("all")
*/
class Manager
{
......
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Serializer\Expose
* @Serializer\Groups({
* "user",
* })
*/
private $id;现在,您决定需要从管理器获得哪些属性,并在每个实体之前添加以下内容
* @Serializer\Expose
* @Serializer\Groups({
* "user",
* })
$property然后在您的getAction之前添加以下内容
/**
* Get User.
*
* @param User $user
*
* @return User
*
* @Route\Get("/users", options={"expose"=true})
*
* @View(serializerGroups={"user"}) \\notice that its the same group name from before Serializer\Groups({"user"})
*
* @ApiDoc(
* ....
* )
*/
public function getAction()yml
它应该看起来像这样
Acme\User:
exclusion_policy: ALL
properties:
id:
expose: true
username:
expose: true
mpks:
expose: true
Acme\Manager
exclusion_policy: ALL
properties:
id:
expose: true
first_name:
expose: true
last_name:
expose: true
#and the rest of your wanted propertieshttps://stackoverflow.com/questions/33037124
复制相似问题