我有一个包含字段的表
int id|text title|text content|int articleCommentCount|
当我访问 json url/articles.json 时,我得到类似的内容
{
"announcements": [
{
"id": 2,
"title":"title",
"articleCommentCount": 16 //Notice value is an int
}
]
}
现在,由于我的 api 的结构,我希望将articleCommentCount 标记为comment_count(仅用于json 输出)。所以我修改了 Controller 中的 find() 并指定了类似的内容;
->select([
'comment_count'=>'articleCommentCount'])
正如您所看到的,articleCommentCount 的值镜像到 comment_count,如下所示
{
"announcements": [
{
"id": 2,
"title":"title",
"articleCommentCount": 16
"comment_count": "16" //Notice value was mirrored BUT is a JSON STRING
}
]
}
问题: 如何避免获取 String 而不是 int
的问题CakePHP 版本 3.2
最佳答案
使用typeMap()
方法
->select(['comment_count'=>'articleCommentCount'])
->typeMap(['comment_count' => 'integer'])
关于CakePHP 3.0 : Cast selected fields to int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37939514/