如何设置别名然后仍然影响Cake 3.x的json格式的查询的返回类型?

时间:2016-03-13 16:48:18

标签: json cakephp cakephp-3.0

情况

使用cake 3.2.4

和插件Crud 4.2

用于生成API提要

我最初使用的是什么代码?

  $this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
        $this->paginate['conditions'] = $conditions;
        $this->paginate['limit'] = 100;
        $this->paginate['fields'] = [
            'id', 'title', 
             'start_date', 
            'end_date', 
            'revenue', 
            'total_costs', 'collections'
        ];
    });

我作为json feed获得了什么?

"success": true,
"data": [
    {
        "id": 789,
        "title": "9143 - Asia Summit",
        "start_date": "2016-03-02T09:00:00+0800",
        "end_date": "2016-03-04T18:45:00+0800",
        "revenue": 1000000.00,
        "total_costs": 0,
        "collections": 50000.00
    },
    {
        "id": 15,
        "title": "9144 - 10th  Exhibition",
        "start_date": "2016-03-21T09:00:00+0800",
        "end_date": "2016-03-23T17:00:00+0800",
        "revenue": 2000000.00,
        "total_costs": 0,
        "collections": 50000.00
    }]}

我接下来做了什么?

我想作为别名返回,所以我做了以下

  $this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
        $this->paginate['conditions'] = $conditions;
        $this->paginate['limit'] = 100;
        $this->paginate['fields'] = [
            'id', 'title', 
            'start' => 'start_date', 
            'end' => 'end_date', 
            'revenue', 
            'costs' => 'total_costs', 'collections'
        ];
    });

我现在得到了什么?

{
"success": true,
"data": [
    {
        "id": 789,
        "title": "9143 - Asia Summit",
        "start": "2016-03-02 09:00:00",
        "end": "2016-03-04 18:45:00",
        "revenue": 1000000.00,
        "costs": "0.00",
        "collections": 50000.00
    },
    {
        "id": 15,
        "title": "9144 - 10th Exhibition",
        "start": "2016-03-21 09:00:00",
        "end": "2016-03-23 17:00:00",
        "revenue": 2000000.00,
        "costs": "0.00",
        "collections": 50000.00
    },

那有什么不对?

我得到了我想要的别名,但是现在成本变成了一个字符串。

日期时间不再显示时区。

如何在保留别名的同时强制json Feed中对象的字段为某种类型?

1 个答案:

答案 0 :(得分:0)

意识到在3.2中你可以使用addDefaultTypes直接影响查询

请参阅Cakephp-3.x: How to change the data type of a selected alias?

意识到您可以在使用Crud

时从$ event访问查询

请参阅http://crud-view.readthedocs.org/en/latest/basic-usage.html?highlight=query#providing-associations-to-be-displayed

使用类型datetime和decimal

把它放在altogther上,你得到:

$this->Crud->on('beforePaginate', function(Event $event) use ($conditions) {
        $this->paginate['conditions'] = $conditions;
        $this->paginate['limit'] = 100;
        $this->paginate['fields'] = [
            'id', 'title', 
            'start' => 'start_date', 
            'end' => 'end_date', 
            'revenue', 
            'costs' => 'total_costs', 'collections'
        ];
        $paginationQuery  = $event->subject()->query;

        $paginationQuery
        ->selectTypeMap()
        ->addDefaults([
            'start' => 'datetime',
            'end' => 'datetime',
            'costs' => 'decimal'
        ]);

    });