Skip to content

Commit 1e7fc8d

Browse files
committed
fix: improve get tickets generic pagination
1 parent ead4fd4 commit 1e7fc8d

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

app/Repositories/DoctrineRepository.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ protected function getParametrizedAllByPage
196196
Log::debug(sprintf("DoctrineRepository::getParametrizedAllByPage DQL %s", $query->getDQL()));
197197
$start = time();
198198

199-
$paginator = new Paginator($query, $fetchJoinCollection = true);
199+
$paginator = new Paginator($query, $this->fetchJoinCollection);
200200
$total = $paginator->count();
201201
$end = time();
202202
$delta = $end - $start;
@@ -290,7 +290,7 @@ public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Ord
290290
->setFirstResult($paging_info->getOffset())
291291
->setMaxResults($paging_info->getPerPage());
292292

293-
$paginator = new Paginator($query, $fetchJoinCollection = true);
293+
$paginator = new Paginator($query, $this->fetchJoinCollection);
294294
$total = $paginator->count();
295295
$data = [];
296296

@@ -307,6 +307,35 @@ public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Ord
307307
);
308308
}
309309

310+
/**
311+
* @param Filter|null $filter
312+
* @param Order|null $order
313+
* @return int
314+
*/
315+
public function getFastCount(Filter $filter = null, Order $order = null){
316+
$query = $this->getEntityManager()
317+
->createQueryBuilder()
318+
->distinct(true)
319+
->select('COUNT(DISTINCT e.id)')
320+
->from($this->getBaseEntity(), "e");
321+
322+
$query = $this->applyExtraJoins($query, $filter, $order);
323+
324+
$query = $this->applyExtraSelects($query, $filter, $order);
325+
326+
if(!is_null($filter)){
327+
$filter->apply2Query($query, $this->getFilterMappings($filter));
328+
}
329+
330+
$query = $this->applyExtraFilters($query);
331+
332+
if(!is_null($order)){
333+
$order->apply2Query($query, $this->getOrderMappings($filter));
334+
}
335+
336+
return (int) $query->getQuery()->getSingleScalarResult();
337+
}
338+
310339
/**
311340
* @param PagingInfo $paging_info
312341
* @param Filter|null $filter

app/Repositories/Summit/DoctrineSummitAttendeeTicketRepository.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Doctrine\ORM\EntityManagerInterface;
2020
use Doctrine\ORM\Mapping\ClassMetadata;
2121
use Doctrine\ORM\QueryBuilder;
22+
use Illuminate\Support\Facades\Log;
2223
use models\summit\IOrderConstants;
2324
use models\summit\ISummitAttendeeTicketRepository;
2425
use models\summit\ISummitRefundRequestConstants;
@@ -32,6 +33,7 @@
3233
use utils\Filter;
3334
use utils\Order;
3435
use utils\PagingInfo;
36+
use utils\PagingResponse;
3537

3638
/**
3739
* Class DoctrineSummitAttendeeTicketRepository
@@ -89,7 +91,7 @@ private function ensureJoin(QueryBuilder $qb, string $alias): void
8991
*/
9092
private function requiredAliases(?Filter $filter, ?Order $order): array
9193
{
92-
$need = ['a' => true]; // owner always
94+
$need = []; // owner always
9395

9496
$has = fn(string $f) => $filter?->hasFilter($f) ?? false;
9597
$ord = fn(string $f) => $order?->hasOrder($f) ?? false;
@@ -425,10 +427,6 @@ protected function applyExtraJoins(QueryBuilder $query, ?Filter $filter = null,
425427
$this->ensureJoin($query, $alias);
426428
}
427429

428-
// owner is always selected to prevent N+1
429-
if (\in_array('a', $query->getAllAliases(), true)) {
430-
$query->addSelect('a'); // to-one fetch join is safe with pagination
431-
}
432430

433431
return $query;
434432
}
@@ -704,4 +702,45 @@ public function getAllTicketsIdsByOrder(int $order_id, PagingInfo $paging_info):
704702
$res = $query->getQuery()->getArrayResult();
705703
return array_column($res, 'id');
706704
}
705+
706+
707+
/**
708+
* @param PagingInfo $paging_info
709+
* @param Filter|null $filter
710+
* @param Order|null $order
711+
* @return PagingResponse
712+
*/
713+
public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Order $order = null){
714+
$start = time();
715+
Log::debug(sprintf('DoctrineSummitAttendeeTicketRepository::getAllByPage'));
716+
$total = $this->getFastCount($filter, $order);
717+
$ids = $this->getAllIdsByPage($paging_info, $filter, $order);
718+
$query = $this->getEntityManager()->createQueryBuilder()
719+
->select('e, a, o, tt, pc, b, bt, a_c, m')
720+
->from($this->getBaseEntity(), 'e')
721+
->leftJoin('e.owner', 'a')->addSelect('a')
722+
->leftJoin('e.order', 'o')->addSelect('o')
723+
->leftJoin('e.ticket_type', 'tt')->addSelect('tt')
724+
->leftJoin('e.promo_code', 'pc')->addSelect('pc')
725+
->leftJoin('e.badge', 'b')->addSelect('b')
726+
->leftJoin('b.type', 'bt')->addSelect('bt')
727+
->leftJoin('a.company', 'a_c')->addSelect('a_c')
728+
->leftJoin('a.member', 'm')->addSelect('m')
729+
->where('e.id IN (:ids)')
730+
->setParameter('ids', $ids);
731+
732+
$data = [];
733+
foreach( $query->getQuery()->getResult() as $entity)
734+
array_push($data, $entity);
735+
$end = time() - $start;
736+
Log::debug(sprintf('DoctrineSummitAttendeeTicketRepository::getAllByPage %s seconds', $end));
737+
return new PagingResponse
738+
(
739+
$total,
740+
$paging_info->getPerPage(),
741+
$paging_info->getCurrentPage(),
742+
$paging_info->getLastPage($total),
743+
$data
744+
);
745+
}
707746
}

0 commit comments

Comments
 (0)