doy2255 2016-02-25 22:07
浏览 18
已采纳

在页面API请求中包含相关的模型数据

I've got a relatively simple setup running using SilverStripe 3.2.1 with the restfulserver addon and using a variety of widgets which are associated to a Page using the elemental addon.

When I make a GET request via the API to retrieve some of Page #1's data, I can see the associated ElementAreaID:

# GET /api/v1/Page/1.json?fields=Title,URLSegment,Content,ElementArea 
{
  "Title": "Welcome",
  "URLSegment": "home",
  "Content": "A bunch of HTML here from all the widgets in the page...",
  "ElementArea": {
    "className": "ElementalArea",
    "href": "http://ss.local:3000/api/v1/ElementalArea/11.json",
    "id": "11"
  }
}

If I follow the links through the ElementalArea API calls it will list out all of the elements in my page:

# GET /api/v1/ElementalArea/11.json
{
  "ID": "11",
  "Widgets": [
    {
      "className": "Widget",
      "href": "http://ss.local:3000/api/v1/Widget/9.json",
      "id": 9
    },
    {
      "className": "Widget",
      "href": "http://ss.local:3000/api/v1/Widget/8.json",
      "id": 8
    },
    ...
  ]
}

And if I follow those API paths it will serve up the contents of the latest version of each of the Widgets.

My question is how can I include certain fields from the Widget DataObjects within the original Page field list?

I'd like ideally to have the Content field from each Widget be returned in an array with the initial Page API request.


For reference:

  • A Page has one ElementArea
  • An ElementArea has many Widgets
  • A Widget contains content that I want for my Page
  • 写回答

1条回答 默认 最新

  • dspld86684 2016-03-01 20:22
    关注

    Preamble: It doesn't seem there's currently a way to output array-like datastructures with the RESTful server module (except for relations of course). The proposed solution is a hack that abuses how JSONDataFormatter formats output.

    Since JSONDataFormatter uses forTemplate to render a field before converting it to JSON, we can create our own Object renderer that returns an array instead of a string via forTemplate. This might look like this:

    class FlatJSONDataList extends ViewableData
    {
        protected $list;
    
        public function __construct(array $list)
        {
            parent::__construct();
            $this->list = $list;
        }
    
        public function forTemplate()
        {
            return $this->list;
        }
    }
    

    Then in your Page, it should be sufficient to have an additional method, like so:

    public function getWidgetContents()
    {
        return FlatJSONDataList::create(
            $this->ElementArea()->Widgets()->column('Content')
        );
    }
    

    Then you can include WidgetContents in your Field-list to get all widget Content fields in an array:

    GET /api/v1/Page/1.json?fields=Title,URLSegment,Content,WidgetContents 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿