首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >symfony FOSRestBundle深度

symfony FOSRestBundle深度
EN

Stack Overflow用户
提问于 2015-10-09 19:27:48
回答 1查看 438关注 0票数 1

我使用的是FOSRestBundle --带有自动旋转和自动视图。我在Controller中的操作如下所示:

代码语言:javascript
复制
public function getAction($user_id)
{
    $user = $this->em->getRepository('SBGUserBundle:User')->find($user_id);
    return $user;
}

一切正常,我的JSON格式的响应如下所示:

代码语言:javascript
复制
{
    "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个级别。我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2017-02-15 21:36:44

注解

如果你正在使用注解,你可以像下面的yml配置那样做,以供以后参考。首先,您转到manager实体并添加以下内容:

代码语言:javascript
复制
/**
 * 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;

现在,您决定需要从管理器获得哪些属性,并在每个实体之前添加以下内容

代码语言:javascript
复制
* @Serializer\Expose
* @Serializer\Groups({
*     "user",
* })
$property

然后在您的getAction之前添加以下内容

代码语言:javascript
复制
/**
 * 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

它应该看起来像这样

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33037124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档