php将整型主键(id)转换为字符串
我想在我的sql查询结果中将php将整型主键(id)转换为字符串,php,mysql,cakephp-3.0,Php,Mysql,Cakephp 3.0,我想在我的sql查询结果中将id转换为string 我想用字符串id响应json 我在select中尝试了convert(id,char)和cast(id as char),但问题仍然存在 例如: { "status": 1, "message": "Success", "nexturl": "http://domain.dev/feed/298", "data": [ { "id": 123456789, "userid": 12, }
id
转换为string
我想用字符串id响应json
我在select
中尝试了convert(id,char)
和cast(id as char)
,但问题仍然存在
例如:
{
"status": 1,
"message": "Success",
"nexturl": "http://domain.dev/feed/298",
"data": [
{
"id": 123456789,
"userid": 12,
}
]
}
我想得到这个结果:
{
"status": 1,
"message": "Success",
"nexturl": "http://domain.dev/feed/298",
"data": [
{
"id": "123456789",
"userid": "12",
}
]
}
将(id,CHAR(50))转换为id是正确的语法。我找到了答案
我们可以转换实体类中的列
所以我为用户
表创建了一个实体,并在实体中转换了主键(id
)
namespace App\Model\Entity;
use Cake\ORM\Entity;
class User extends Entity
{
protected $_accessible = [
'*' => true,
];
protected function _getId($id){
return (string) $this->_properties['id'];
}
}
比如?你需要发布(完整的)相关代码以及数据库模式。不要构建你自己的JSON字符串。使用json\u encode($数组或对象)代码>来做它。那就对了。你的可能大部分时间都不会。偶尔使用手册几分钟可以为您节省数小时的痛苦@DominiqueLorre是的,就像那样…可能会重复See更改现有列的类型。这最好在PHP中完成。在查询中这样做是完全没有意义的,因为还涉及到一个附加层。