CakePHP响应数字作为字符串

时间:2020-03-31 14:54:26

标签: cakephp

CakePHP正在json响应中将数字序列化为字符串,例如{{foo“:” 123“},应该是{” foo“:123}有任何配置吗?

    $stm = "select * from clients";
    $response = $conn->execute($stm)->fetchAll('assoc');

    $this->set([
        'success' => $success,
        'code' => $code,
        'response' => $response,
        'errors' => $errors,
        '_serialize' => ['success', 'code', 'response', 'errors'],
    ]);

1 个答案:

答案 0 :(得分:2)

您可以像这样传递json_encode选项

$this->set('_jsonOptions', JSON_NUMERIC_CHECK);
$this->set('_serialize', ['success', 'code', 'response', 'errors']);

https://book.cakephp.org/3/en/views/json-and-xml-views.html#creating-json-views