使用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'
];
});
"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中对象的字段为某种类型?
答案 0 :(得分:0)
请参阅Cakephp-3.x: How to change the data type of a selected alias?
把它放在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'
]);
});