如何设置别名,然后仍然影响查询的json格式的Cake 3.x的返回类型? [英] How to set aliases and then still affect the return type for queries in json formats for Cake 3.x?

查看:250
本文介绍了如何设置别名,然后仍然影响查询的json格式的Cake 3.x的返回类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用蛋糕3.2.4

和插件Crud 4.2

and the plugin 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得到什么?



What am I getting as 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
    }]}



然后我做了什么?



别名,所以我做了以下

What did I then do?

I wanted to return as aliases so I did the following

  $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'
        ];
    });



我现在得到了什么?



What did I now get?

{
"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
    },



那么怎么了?



So what's wrong?

I get the aliases I wanted, but then the costs has now become a string.

日期时间不再显示时区。

The datetime no longer shows the timezone.

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

How do I force the fields of the objects in the json feed to be of a certain type whilst keeping the aliases?

推荐答案

Realize在3.2中,您可以使用addDefaultTypes直接影响查询



请参阅 Cakephp-3.x别名更改find​​方法中的数据类型

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

将其添加到其他位置,您将获得:

Put it altogther and you get:

$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'
        ]);

    });

这篇关于如何设置别名,然后仍然影响查询的json格式的Cake 3.x的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆