0

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",
    }
  ]
}
13
  • 1
    Like this? Commented Sep 29, 2016 at 14:43
  • 2
    you need to post (full) relevant code as well as db schema. Commented Sep 29, 2016 at 14:45
  • 4
    Dont build your own JSON Strings. Use json_encode($an array or an object); to do it. Then it will be correct. Yours probably wont be most of the time. A few minutes now and again with the manual can save you hours of pain And here is the manual Commented Sep 29, 2016 at 14:49
  • @DominiqueLorre yes like that... Commented Sep 29, 2016 at 14:54
  • 1
    Possible duplicate of stackoverflow.com/questions/35650306/… See Change the type of existing columns. Commented Sep 29, 2016 at 18:07

2 Answers 2

0

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'];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-2

CONVERT(id, CHAR(50)) as id is the correct syntax.

1 Comment

This is better done within PHP. It's utterly pointless to do it in the query since there's an additional layer involved anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.