gpt4 book ai didi

php - Symfony 中的 API 分页

转载 作者:行者123 更新时间:2023-12-05 07:21:58 25 4
gpt4 key购买 nike

我正在研究 API,我有一个 Controller 可以向 framework7 应用程序查询结果。 (首先,最后和两者)。然而,我想从 API 添加分页到框架,但我以前没有在 Symfony 中分页,最佳实践是什么?

我试过分页器,但没有太多例子。下面是我的 Controller 功能。

 public function getEventSessionAttendeeAction(request $request, $eventSessionId)
{
$searchFields = [
'o.email',
'a.email',
'a.firstName',
'a.lastName',
'a.barcode1',
'a.barcode2',
'a.id',
'o.id'
];

/** @var \KCM\ApiBundle\Entity\Api\EventSession $eventSession */
$eventSession = $this->get('doctrine')->getRepository('KCMApiBundle:EventSession')->findOneBy(
[
'id' => $eventSessionId
]
);
/** @var ApiEntity\Event $event */
$event = $eventSession->getEvent();
$childSafe = $event->getChildSafe();

$filter = $request->get('filter');
$match_level = 0;

//Searches by email address
if ( filter_var($filter, FILTER_VALIDATE_EMAIL)) {
$searchFields = [
'a.email'
];
$match_level = 1;

//Searches for first or last name
}elseif(preg_match('/^[a-zA-Z\-]*$/', $filter)){
$searchFields = [
'a.lastName',
'a.firstName'
];
$match_level = 2;

//Searches by barcode
}elseif (preg_match('/^[0-9]+/', $filter)){

if ($childSafe == 2) {
$searchFields = [
'a.barcode3'
];
} else {
$searchFields = [
'a.barcode1'
];
}
$match_level = 4;
}

//Searches for first and last name(must have at least first characters for each
if(preg_match('/^([a-z|A-Z]+)\s{1}([a-z|A-Z]+)/', $filter)){
$match_level = 3;
}elseif (preg_match('/^\s*$/', $filter)){
$match_level = 5;
}

try {
/** @var ApiEntity\EventSession $eventSession */

$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$qb->select(array('a', 'o'))
->from('KCMApiBundle:EventAttendee', 'a')
->join('a.eventOrder', 'o')
->join('o.event', 'e')
->join('e.eventSessions', 'es')
->where($qb->expr()->andX(
$qb->expr()->eq('es.id', $qb->expr()->literal($eventSessionId))
))
->andWhere('a.sessionCheckedIn = 1');

if ($match_level === 3){
$expr = $qb->expr()->andX();
list($first, $last) = explode(' ', $filter);
$last = $last. '%';
$expr->add($qb->expr()->like('a.lastName', $qb->expr()->literal($last)));
$first = $first. '%';
$expr->add($qb->expr()->like('a.firstName',$qb->expr()->literal($first)));

}elseif ($match_level === 5){
$expr = $qb->expr()->andX();

$paginator = new Paginator($qb);
$paginator->getQuery()
->setFirstResult(0)
->setMaxResults(10);

}else {
$expr = $qb->expr()->orX();
foreach ($searchFields as $field) {

if ($match_level === 1) {
$literalFilter = $filter;
$expr->add($qb->expr()->like($field, $qb->expr()->literal($literalFilter)));
} elseif ($match_level === 2) {
$literalFilter = $filter;
$expr->add($qb->expr()->like($field, $qb->expr()->literal($literalFilter)));
} elseif ($match_level === 4) {
$literalFilter = $filter;
$expr->add($qb->expr()->eq($field, $qb->expr()->literal($literalFilter)));
}
}
}

$qb->andWhere($expr);
$results = $qb->getQuery()->getResult();


if ($results) {
return $this->getApi()->serialize($results);
}

return new Response(null, Response::HTTP_NOT_FOUND);

} catch (\Exception $e) {
$this->get('logger')->error($e->getMessage());
}
}

我最终想做的是能够使用此查询分页到 framework7 应用程序。我应该创建一个新的公共(public)函数来分页还是有办法在这个函数中做到这一点?

最佳答案

您是否尝试过使用 knppagination 包?

https://github.com/KnpLabs/KnpPaginatorBundle

关于php - Symfony 中的 API 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56690335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com