mostafaznv mostafaznv - 2 months ago 9
MySQL Question

php convert integer primary key (id) to string

i want to convert

id
to
string
in my sql query result.

i want to response json with string id ...

i tried
convert(id, char)
and
cast(id as char)
in
select
but problem remains ...

example:

{
"status": 1,
"message": "Success",
"nexturl": "http://domain.dev/feed/298",
"data": [
{
"id": 123456789,
"userid": 12,
}
]
}


i want to get this result:

{
"status": 1,
"message": "Success",
"nexturl": "http://domain.dev/feed/298",
"data": [
{
"id": "123456789",
"userid": "12",
}
]
}

Answer

i found the answer

we can convert columns in entity class...

so i created an entity for users table and i converted the primary key (id) in entity.

namespace App\Model\Entity;

use Cake\ORM\Entity;

class User extends Entity
{
    protected $_accessible = [
       '*' => true,
    ];

    protected function _getId($id){
        return (string) $this->_properties['id'];
    }
}
Comments