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",
    }
  ]
}
mostafaznv
  • 958
  • 14
  • 24
  • 1
    Like [this](http://stackoverflow.com/questions/12821528/int-to-string-in-mysql)? – Dominique Lorre Sep 29 '16 at 14:43
  • 2
    you need to post (full) relevant code as well as db schema. – Funk Forty Niner Sep 29 '16 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](http://php.net/manual/en/function.json-encode.php) – RiggsFolly Sep 29 '16 at 14:49
  • @DominiqueLorre yes like that... – mostafaznv Sep 29 '16 at 14:54
  • possible duplicate of [int to string in mysql](http://stackoverflow.com/questions/12821528/int-to-string-in-mysql) – Funk Forty Niner Sep 29 '16 at 14:55
  • @Fred-ii- i have an table with auto increment `id` ... and i'm using query builder in cakephp 3 for get result... – mostafaznv Sep 29 '16 at 14:56
  • @RiggsFolly i didn't build json manually...i'm using json_encode ... – mostafaznv Sep 29 '16 at 14:57
  • Why do you need the `id` as a string and not a number may I ask? – RiggsFolly Sep 29 '16 at 14:59
  • i want to use json in android, ios and windows mobile ... integer may have 32 or 64 bits ... i want to prevent overflow in some platforms ... @RiggsFolly – mostafaznv Sep 29 '16 at 15:05
  • @mostafaznv You should double check you actually need 64 bits because 99% of the time you don't. If you're Twitter, Facebook or Google, *sure*, it makes sense, but 2.1 billion identifiers take a long time to exhaust for more ordinary applications. Hint: [`strval`](http://php.net/manual/en/function.strval.php). – tadman Sep 29 '16 at 15:13
  • @tadman yes i know ... but my boss insists to response string ids :D – mostafaznv Sep 29 '16 at 15:19
  • Then `strval` it is. You didn't post the code where you render the JSON, but as RiggsFolly says, use `json_encode`. Where you have an integer value now, convert it when passing it through. – tadman Sep 29 '16 at 15:21
  • 1
    Possible duplicate of http://stackoverflow.com/questions/35650306/cakephp-3-x-how-to-change-the-data-type-of-a-selected-alias/35652166 See **Change the type of existing columns**. – ndm Sep 29 '16 at 18:07

2 Answers2

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'];
    }
}
mostafaznv
  • 958
  • 14
  • 24
-2

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

A J
  • 3,970
  • 14
  • 38
  • 53
jimmy8ball
  • 746
  • 5
  • 15
  • This is better done within PHP. It's utterly pointless to do it in the query since there's an additional layer involved anyway. – tadman Sep 29 '16 at 15:12