Symfony2 Hateoas - 我的嵌入式资源在我的序列化数据中出现两次

时间:2016-05-08 17:05:11

标签: symfony doctrine-orm hateoas jmsserializerbundle

我实际上使用了来自Will Durand的HateoasBundle的Symfony2,我想念嵌入式资源。

我有2个实体,User和BlogArticle。 BlogArticle包含"用户"具有ManyToOne关系的属性。

class BlogArticle implements ResourceInterface
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * ORM\Column(name="user_id", type="integer", nullable=false)
     *
     * @ORM\ManyToOne(targetEntity="Kazooi\ApiBundle\Entity\User", fetch="EAGER")
     * @ORM\JoinColumn(name="user_id", nullable=false)
     */
    private $user;

    ...
}

这是我的文件中的序列化数据" Entity.BlogArticle.yml" :

Kazooi\ApiBundle\Entity\BlogArticle:
    exclusion_policy: none
    relations:
        -
            rel: self
            href:
                route: kazooi_api_blog_article_show
                parameters:
                    id: expr(object.getId())
                absolute: true
        -
            rel: user
            href:
                route: kazooi_api_user_show
                parameters:
                    id: expr(object.getUser())
                absolute: true
            embedded:
                content: expr(object.getUser())
            exclusion:
                exclude_if: expr(object.getUser() === null)

结果是:

{
  "id": 1,
  "user": {
    "id": 1,
    "username": "coolfarmer",
    "username_canonical": "coolfarmer",
    "email": "test@hotmail.com",
    "email_canonical": "test@hotmail.com",
    "deleted": false,
    "_links": {
      "self": {
        "href": "http://api.kazooi.dev/1.0/users/1"
      }
    }
  },
  "title": "Ouverture de Kazooi",
  "message": "Lorém ipsum dolor sit amet, consectetur adipisicing elit",
  "date": "2016-02-24T00:00:00+0000",
  "tags": "one, two, three",
  "deleted": false,
  "_links": {
    "self": {
      "href": "http://api.kazooi.dev/1.0/blog/articles/1"
    },
    "user": {
      "href": "http://api.kazooi.dev/1.0/users/1"
    }
  },
  "_embedded": {
    "user": {
      "id": 1,
      "username": "coolfarmer",
      "username_canonical": "coolfarmer",
      "email": "test@hotmail.com",
      "email_canonical": "test@hotmail.com",
      "deleted": false,
      "_links": {
        "self": {
          "href": "http://api.kazooi.dev/1.0/users/1"
        }
      }
    }
  }
}

所以,问题是用户数据在用户属性中出现两次(因为学说关系ManyToOne),并且由于我的Hateoas关系定义而出现在_embedded中。

这是正常的吗?我做错了什么或错过了解。 我认为结果应该是相同的,但用户属性等于" 1"而不是数据。

PS:LAZY或EXTRA LAZY中的实体不会改变结果。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

docs中所述,您需要排除嵌入属性以避免重复。

因此,如果您要保留world.step(timestep, 3, 3); for(Body b : world.getBodies()) if("DESTROY".equals(b.getUserData()) world.destroyBody(body); 下的对象,则会排除_embedded属性:

user

如果您希望保持数据不变并摆脱Kazooi\ApiBundle\Entity\BlogArticle: exclusion_policy: none relations: ... - rel: user href: route: kazooi_api_user_show parameters: id: expr(object.getUser()) absolute: true embedded: content: expr(object.getUser()) exclusion: exclude_if: expr(object.getUser() === null) properties: user: exclude: true 部分,只需从关系中删除_embedded密钥:

embedded