Cakephp 3.0:将选定字段强制转换为int
我有一张有字段的桌子Cakephp 3.0:将选定字段强制转换为int,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我有一张有字段的桌子 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的结构,我希望art
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\u count(仅用于json输出)。所以我在控制器中修改了find()并指定了如下内容:
->select([
'comment_count'=>'articleCommentCount'])
正如您所见,articleCommentCount的值被镜像到comment\u count,如下所示
{
"announcements": [
{
"id": 2,
"title":"title",
"articleCommentCount": 16
"comment_count": "16" //Notice value was mirrored BUT is a JSON STRING
}
]
}
问题:
如何避免获取字符串而不是int的问题
CakePHP版本3.2使用typeMap()
方法
->select(['comment_count'=>'articleCommentCount'])
->typeMap(['comment_count' => 'integer'])
应该注意的是,这将覆盖完整的类型映射,导致其他字段丢失其类型映射。此外,类型映射也在其他地方使用,不仅用于强制转换选定字段,这就是为什么可能需要使用
selectTypeMap()
。