0
URLに指定されたパラメータに応じてサービスのリストを返すメソッドを実装するにはどうすればよいですか?Symfony REST API:複数のパラメータを持つクエリ
パラメータがない場合、すべてのサービスが返されます。ユーザーとカテゴリが指定されている場合は、両方のパラメータでフィルタリングします。ユーザーまたはカテゴリのみが提供されている場合は、いずれかのパラメータでフィルタします。
/**
* @Route("/", name="api_services_search")
* @Method("GET")
* @ApiDoc(
* section = "Service",
* description="Search services",
* parameters={
* {"name"="category", "dataType"="int", "required"=true, "description"="Category ID"}
* {"name"="user", "dataType"="int", "required"=true, "description"="User ID"}
* },
* output="CoreBundle\Entity\Service"
*)
*/
public function searchAction(Request $request){
$categoryId = $request->query->get('category');
$userId = $request->query->get('user');
$result = new JsonResponse();
if($categoryId){
$category = $this->getDoctrine()->getRepository('CoreBundle:ServiceCategory')->find($categoryId);
if($category == null){
throw new ApiException('category not found');
}
$serviceList = $this->getDoctrine()
->getRepository('CoreBundle:Service')->findBy(array('serviceCategory' => $category));
}
else if($userId){
$user = $this->getDoctrine()->getRepository('CoreBundle:BasicUser')->find($userId);
if($user == null){
throw new ApiException('user not found');
}
$serviceList = $this->getDoctrine()
->getRepository('CoreBundle:Service')->findBy(array('basicUser' => $user));
} else{
$serviceList = $this->getDoctrine()
->getRepository('CoreBundle:Service')->findAll();
}
$serviceListJson = $this->serializeDataObjectToJson($serviceList);
$result->setContent($serviceListJson);
return $result;
}
例URL:
http://127.0.0.1:8000/api/v1/services/?category=3&user=4
私がエラーを持っている:
Too many parameters: the query defines 1 parameters and you bound 2 (500 Internal Server Error)
はまた、私は、私は簡単に将来
この例では何が問題になりますか? – VaN
@VaN私のコードは非常に醜く非効率的です。 URLにパラメータを追加するとどうなりますか?私はもっと清潔な解決策があると思った – blahblah