I created a custom Bundle in my Symfony 2.7 website. One of the entity works perfectly :
- Entity class is Version.php
- Custom repository is VersionRepository
The MainController.php of my bundle is :
$repository = $this->getDoctrine()->getManager()->getRepository('MyBundle:Version');
$versions = $repository->findAll();
return $this->render('IpadUpdaterBundle:Version:index.html.twig', array(
'versions' => $versions
));
Output in Twig is perfect :
- My first version row
- My second version row
BUT ... if I want to change the ouput and render a JSON response with the same datas, I make this change in the controller :
$repository = $this->getDoctrine()->getManager()->getRepository('MyBundle:Version');
$versions = $repository->findAll();
$versions = json_encode($versions);
$rep_finale = new Response($versions);
$rep_finale->headers->set('Content-Type', 'application/json');
return $rep_finale;
or :
$repository = $this->getDoctrine()->getManager()->getRepository('MyBundle:Version');
$versions = $repository->findAll();
return new JsonResponse($versions);
.. and the ouput becomes an empty array with 2 children :
[{},{}]
!I can not understand what's wrong and what changes I would implement to solve this issue. I already use "use Symfony\Component\HttpFoundation\Response" and "use Symfony\Component\HttpFoundation\JsonResponse" in the header of my controller.php.
Thanks for your help !