我尝试使用
public function listeleAction(Request $ request) { $ EM = $这 - > getDoctrine() - > getManager();
$yasam_list=$em->getRepository("VanBundle:Yasam")->findAll();
if (count($yasam_list)>0)
{
$yanit=array();
foreach ($yasam_list as $meta)
{
$new_yasam=array();
$new_yasam['id']=$meta->getId();
$new_yasam['adi']=$meta->getAdi();
$new_yasam['adres']=$meta->getAdres();
$new_yasam['kategori_id']=$meta->getKategori()->getAdi();
array_push($yanit,$new_yasam);
}
$paginator=$this->get('knp_paginator');
$result= $paginator->paginate(
$yanit,
$request->query->getInt('page',1),
$request->query->getInt('limit',10)
);
}else{
$yanit["success"] = 0;
$response["message"] = "Bu kategoriye en kısa sürede veri eklenecektir. ";
}
$response=new Response(json_encode(array('yasamlar' =>$result),JSON_UNESCAPED_UNICODE));
$response->headers->set('Content-type','application/json; charset=utf-8');
$response->setStatusCode(200);
return $response;
}
我的json输出
{
"yasamlar": {}
}
我该如何解决这个问题?谢谢所有
答案 0 :(得分:1)
在加入分页之前,很少有建议:
KnpPaginatorBundle
和doctrine查询构建器。运行完整查询并从结果中进行分页无助于即兴表现。json_encode
,设置Content-Type
或Status Code
。JsonResponse
代替Response
。现在,关于将KnpPagination
信息传递给API:
如果您使用的是FosRestBundle,则可以使用JMSSerializerBundle来帮助您将实体信息解析为JSON
以获取API响应。您可以进一步选择要公开的属性。分页数据将自动解析。
否则,您只需要遍历KNPPaginator $result
对象,您将获得表单原则查询构建器,并将分页和数据分别发送到JsonResponse
。
如果您需要代码段,请告诉我。
希望这有帮助!
答案 1 :(得分:0)
我解决了
public function indexAction(Request $request)
{
$em=$this->getDoctrine()->getManager();
//listelenecek bütün Yasam
$yasam_list=$em->getRepository("VanBundle:Yasam")->findAll();
$pager=$this->get('knp_paginator');
$paginated=$pager->paginate($yasam_list,$request->query->getInt('page',1), $request->query->getInt('limit',10));
$factory=new KnpPaginatorFactory();
$collection=$factory->createRepresentation($paginated,new Configuration\Route('yasamjson_index'),array());
$serializer = $this->get('jms_serializer');
$data = $serializer->serialize([
'meta' => $collection,
'data' => $paginated->getItems()
], 'json');
return new Response($data);
}