7

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
  • I think the problem is you are trying to fetch multiple data records into one response. The syntax needs to be new JsonResponse(array('name' => $name));. Commented Nov 19, 2014 at 6:35

1 Answer 1

24

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

1 Comment

Ok thats good method but what when result from my code insert to Twig and it doesn`t work too ? In twig result from my code should work...

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.