简体   繁体   中英

How do I convert an integer to string in CakePHP?

In my CakePHP app when I try to get data like this:

    $this->loadModel('Radio');
    $posts = $this->Radio->find('all');

the integers are displayed like strings (in debug) :

'Radio' => array(
    'idSong' => '4',
    'name' => 'batman',
    'title' => 'Batman Theme Song'
),

why? the type is int in the DB. I need integers correctly displayed in my JSON files

Not sure if there's a straightforward solution, but you could change the model data using afterfind

Something like

public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
        if (isset($val['Radio']['idSong'])) {
            $results[$key]['Radio']['idSong'] = (int)$results[$key]['Radio']['idSong'];
        }
    }
    return $results;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM