簡體   English   中英

Symfony REST API:具有多個參數的查詢

[英]Symfony REST API: query with multiple parameters

如何實現根據URL中提供的參數返回服務列表的方法?

因此,如果沒有參數,則返回所有服務。 如果提供了用戶和類別,則按兩個參數進行過濾。 如果僅提供用戶或僅提供類別,則按參數之一進行過濾。

/**
 * @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;
}

范例網址:

  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)

我也在尋找可維護的解決方案,將來可以在其中輕松地向URL添加更多參數

我會選擇這樣的東西。 恐怕您無法通過查詢參數添加/編輯使它更通用且不可知。

/**
 * Controller action
 */
public function searchAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();


    $serviceList = $em->getRepository('CoreBundle:Service')->fetchFromFilters([
        'serviceCategory' => $request->query->get('category'),
        'basicUser' => $request->query->get('user'),
    ]);

    $serviceListJson = $this->serializeDataObjectToJson($serviceList);

    $result = new JsonResponse();
    $result->setContent($serviceListJson);
    return $result;
}

/**
 * Repository fetching method
 */
public function fetchFromFilter(array $filters)
{
    $qb = $this->createQueryBuilder('s');

    if (null !== $filters['serviceCategory']) {
        $qb
            ->andWhere('s.serciceCategory = :serviceCategory')
            ->setParameter('serviceCategory', $filters['serviceCategory'])
        ;
    }
    if (null !== $filters['basicUser']) {
        $qb
            ->andWhere('s.basicUser = :basicUser')
            ->setParameter('basicUser', $filters['basicUser'])
        ;
    }

    return $qb->getQuery()->getResult();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM