我有一个返回服务列表的 REST GET 方法。通过在查询链接中提供参数,我希望能够对列表进行排序。例如:sort=price
- 按价格升序排序列表,如果sort=-price
- 降序排序
查询看起来像:
http://127.0.0.1:8000/api/v1/services?sort=price
方法
public function searchAction(Request $request){
$sort = $request->query->get('sort');
$page = $request->query->getInt('page', 1);
$limit = $request->query->getInt('limit', 5);
$result = new JsonResponse();
$qb = $this->getDoctrine()->getRepository('AppBundle:Service')->createQueryBuilder('s');
if($sort != null){
if($sort == 'price'){
$qb->addOrderBy('s.price', 'ASC');
}
elseif($sort == '-price'){
$qb->addOrderBy('s.price', 'DESC');
}
}
$paginator = $this->get('knp_paginator');
$serviceListJson = $paginator->paginate(
$qb,
$page,
$limit
);
$result->setContent($serviceListJson);
return $result;
}
我收到错误
There is no component field [price] in the given Query (500 Internal Server Error)
服务实体
class Service{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="title", type="string")
*/
private $title;
/**
* @var int
*
* @ORM\Column(name="price", type="integer")
*/
private $price;
}
我是using KNP PAginator
捆绑