Symfony中的查询过滤器API

时间:2019-06-11 18:13:47

标签: api symfony filter doctrine query-builder

我正在尝试创建一个端点,该端点从过滤器列表中查询并吐出结果。下面的代码将所有结果发送回去,但是我正在尝试创建可以使用$ filter = moreno进行查询的内容。

这是我的控制者。该控制器查询所有当前会话

  /**
     * Get attendees in a session.
     *
     * @Get("/eventSession/{eventSessionId}/eventSession")
     *
     * @View(serializerGroups={"Default"})
     * @ApiDoc(
     *  output= {
     *      "class" = "array<KCM\ApiBundle\Contract\EventAttendee>",
     *      "groups" = {"Default"}
     *  },
     *  requirements={
     *    {
     *      "name"="eventSessionId",
     *      "dataType"="string",
     *      "requirement"="(\w|-)+",
     *      "description"="Event Session ID"
     *    },
     *    {
     *      "name"="filter",
     *      "dataType"="string",
     *      "requirement"="(\w|-)+",
     *      "description"="Value to search for within fields"
     *    }
     *  }
     * )
     *
     * @Secure(roles="api.event_admin")
     */
    public function getEventSessionAttendeeAction(request $request, 
$eventSessionId)
    {

        $filterName = 'filter';

        $searchFields = [
            'o.email',
            'a.email',
            'a.firstName',
            'a.lastName',
            'a.barcode1',
            'a.barcode2',
            'a.id',
            'o.id'
        ];

        try {
          /** @var ApiEntity\EventSession $eventSession */
          $eventSession = $this->get('doctrine')- 
 >getRepository('KCMApiBundle:EventSession')- 
  >findOneById($eventSessionId);
          $eventAttendees = $this- 
  >getEventSessionAttendeesCheckedIn($eventSession);

            if ($eventAttendees) {

                return $this->getApi()->serialize($eventAttendees);
            }

            return new Response(null, Response::HTTP_NOT_FOUND);
        } catch (\Exception $e) {
            $this->get('logger')->error($e->getMessage());
        }
    }

我想做的是能够添加一个过滤器参数,以便我可以根据姓名,姓氏,电话号码等查询结果。

0 个答案:

没有答案