Serializer和Symfony2实现模型侧面加载

Serializer和Symfony2实现模型侧面加载

本文介绍了如何使用JMS Serializer和Symfony2实现模型侧面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为用于UI和Symfony2的Ember.js和Ember数据构建一个项目,用于后端JSON API的FOSRestBundle和JMS Serializer。 JMS Serializer始终在其输出中嵌入嵌套模型,但Ember数据。我找不到任何将JMS Serializer配置为侧装模型而不是嵌入的示例。



当然,我可以只是,但是我想获得侧面加载数据的好处,而不仅仅是解决JMS Serializer中的(潜在)限制。



这就是嵌入的模型数据, em> JMS-Serializer现在做什么:

  {
post:{
id:1,
name:Test Post,
comments:[
{
id:1,
:真棒帖子,男人!
},{
id:2,
comment:是的,他说的话。
}
]
}
}

是我的意思是由侧面加载的模型数据,这是我想要的:

  {
post:{
id:1,
name:Test Post,
comments:[1,2]
}
comments:[
{
id:1,
comment:Awesome post,man!
},{
id:2,
comment:是的,他说的话。
}
]
}

有人知道配置实现我想要的或者有人在JMS-Serializer中实现了这个功能?

解决方案

有一个bundle支持一些更多的功能,如异步加载和一些更灵活的实施和安全功能。




I'm building a project with Ember.js and Ember-data for the UI and Symfony2, FOSRestBundle and JMS Serializer for the backend JSON API. JMS Serializer always embeds nested models in its output, but Ember-data requires that the models are side-loaded. I can't find anywhere an example of configuring JMS Serializer to side-load models rather than embedding them.

Of course, I could just write an adapter on the Ember-data side to transform the result, but I want to gain the benefits of side-loading data and not just work around a (potential) limitation in JMS Serializer.

This is what I mean by embeded model data, which is what JMS-Serializer does now:

{
  "post": {
    "id": 1,
    "name": "Test Post",
    "comments": [
      {
        "id": 1,
        "comment": "Awesome post, man!"
      }, {
        "id": 2,
        "comment": "Yeah, what he said."
      }
    ]
  }
}

This is what I mean by side-loaded model data, which is what I want:

{
  "post": {
    "id": 1,
    "name": "Test Post",
    "comments": [1, 2]
  },
  "comments": [
    {
      "id": 1,
      "comment": "Awesome post, man!"
    }, {
      "id": 2,
      "comment": "Yeah, what he said."
    }
  ]
}

Does anyone know of a configuration to achieve what I want? Or has anyone implemented this functionality in JMS-Serialiser?

解决方案

There is a bundle which supports some more features like async loading and some more flexible implementing and security functions.

https://github.com/UniqueLibs/ember-data-serializer-bundle

这篇关于如何使用JMS Serializer和Symfony2实现模型侧面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 10:21