Hello,
I put \Phalcon\Mvc\Model::setup(['castOnHydrate' => true]);
in the bootstrap file (public/index.php) and when I specify some columns in the find it doesn't work :
// function to return json response
public function JSONBuilder($data, $code) {
$response = new Response();
$message = $this->getResponseDescription($code);
$response->setHeader('Content-Type', 'application/json');
$response->setStatusCode($code, $message);
$response->setJsonContent($data);
return $response;
}
// In my Action
$offices = Offices::find(array(
"columns" => "id, code, description, email"
));
if (count($offices) < 1) {
return $tools->JSONBuilder(array("error" => "Offices not found"), 404);
}
return $tools->JSONBuilder($offices->toArray(), 200);
So the cast is ignored and I'll get :
[
{
"id": "2",
"code": "ANTB06",
"description": "desc2",
"email": "[email protected]"
},
{
"id": "6",
"code": "BATGN",
"description": "desc6",
"email": "[email protected]"
}
...
]
But if I remove ->toArray()
and I remove the columns key for the select it will works.
$offices = Offices::find();
if (count($offices) < 1) {
return $tools->JSONBuilder(array("error" => "Offices not found"), 404);
}
return $tools->JSONBuilder($offices, 200);
I got my id property to integer :
[
{
"id": 2,
"code": "ANTB06",
"description": "desc2",
"email": "[email protected]"
},
{
"id": 6,
"code": "BATGN",
"description": "desc6",
"email": "[email protected]"
}
...
]
Can you tell me why ?