symfony2 empty jsonResponse

2019-06-21 20:16发布

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 ?

1条回答
欢心
2楼-- · 2019-06-21 20:20

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);
查看更多
登录 后发表回答