您好我正在尝试使用symfony2生成json作为响应。
这是我的代码。
/**
* @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);
}
不幸的是我得到了回应:
[[],[],[],[]]
有什么建议吗?
答案 0 :(得分:0)
试试这个,创建一个文件:CommentRepository
object CheckCommand "cas" {
import "plugin-check-command"
command = [ PluginDir + "/icinga-curl_cas.sh" ]
arguments = {
"-u" = {
value = "$curl_casURL$"
description = "URL for curl"
}
"-g" = {
value = "$curl_casGREP$"
description = "GREP for curl"
}
}
}
如您所见,我返回一个数组,现在您可以看到响应的值。 稍后,更改操作控制器中的下一行:
class CommentRepository extends EntityRepository
{
public function findCommentsList($id)
{
$query = $this->getEntityManager()->createQuery('YOUR QUERY HERE');
...
return $query->getArrayResult();
}
}
注意:您应该以单数形式调用您的实体