0

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

1

1 Answer 1

1

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

3 Comments

I put your code and result is the same : monde-du-rat.fr/API/moulinette/radio/posts (see debug), in json too monde-du-rat.fr/API/moulinette/radio/posts.json
maybe add var_dump($results) inside afterFind and see how the records look? I assume the afterfind function is in the right model, right?
Yes, i made a typo mistake :p thks !

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.