我正在使用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": []
}]
}
但我必须更多地采用1级深度 - 我需要“mpks”数组中的“管理员”。 但在其他情况下我不需要3级。 我该怎么办?
答案 0 :(得分:1)
如果您正在使用注释,您可以像以后的yml配置那样进行参考。 首先,您转到经理实体并添加以下内容:
/**
* 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()
它应该看起来像这样
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 properties