简体   繁体   中英

symfony2 empty jsonResponse

i have problem with JsonResponse. Here is my code:

$repo = $this->getDoctrine()->getRepository($repoName);
$users = $repo->findAll();

return new JsonResponse($users);

So when I use var_dump($users) I have arrays with all data, but JsonResponse return me empty arrays. Does anyone know what could have become ?

This is because of serialization to json. JsonResponse uses json_encode method underneath. You have array of entities which php doesn't know how to serialize.

So what you need is a plain array. To get it you need to use getArrayResult()

$repo = $this->getDoctrine()->getRepository($repoName);
$users = $repo->createQueryBuilder('q')
           ->getQuery()
           ->getArrayResult();

return new JsonResponse($users);

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