Hi I am trying to generate json as response using symfony2.
Here is my code.
/**
* @Route("/viewComments/{taskId}")
*
*/
public function viewCommentsAction($taskId)
{
$commentsList = $this->getDoctrine()->getRepository("AppBundle:Comments")
->findBy(array("task" => $taskId));
if (!$commentsList) {
throw $this->createNotFoundException("non comments to show");
}
$serializer = $this->get('serializer');
$json = $serializer->serialize(
$commentsList,
'json', array('groups' => array('group1'))
);
return new Response($json);
}
unfortunately I am getting response:
[[],[],[],[]]
Any suggestions?
Try this, creates a file: CommentRepository
class CommentRepository extends EntityRepository
{
public function findCommentsList($id)
{
$query = $this->getEntityManager()->createQuery('YOUR QUERY HERE');
...
return $query->getArrayResult();
}
}
as you can see, I return an Array and now you can see values on the response. Later, changes the next line in your Action Controller:
...
$commentsList = $this->getDoctrine()->getRepository("AppBundle:Comments")
->findCommentsList($taskId));
...
Note: You should call your entities in singular