Skip to content

Commit 2acb44b

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

File tree

3 files changed

+93
-12
lines changed

3 files changed

+93
-12
lines changed

app/ModelSerializers/Summit/Registration/SummitAttendeeSerializer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ public function serialize($expand = null, array $fields = [], array $relations =
8484
$serializer_type = $params['serializer_type'];
8585
$summit = $attendee->getSummit();
8686

87-
$attendee->updateStatus();
8887
$beginVotingDate = $params['begin_attendee_voting_period_date'] ?? null;
8988
$endVotingDate = $params['end_attendee_voting_period_date'] ?? null;
9089
$track_group_id = $params['presentation_votes_track_group_id'] ?? null;
@@ -347,4 +346,4 @@ public function serialize($expand = null, array $fields = [], array $relations =
347346
});
348347

349348
}
350-
}
349+
}

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+
->select('COUNT(DISTINCT e.id)')
319+
->from($this->getBaseEntity(), "e")
320+
->distinct(false);
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: 61 additions & 8 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;
@@ -98,12 +100,16 @@ private function requiredAliases(?Filter $filter, ?Order $order): array
98100
// --- Filters ---
99101
if ($has('order_number') || $has('order_id') || $has('order_owner_id') || $has('bought_date') || $has('summit_id')) {
100102
$need['o'] = true;
103+
if($has('order_owner_id')){
104+
$this->joinCatalog['ord_m'][1] = 'join';
105+
$need['ord_m'] = true;
106+
}
101107
}
102108
if ($has('summit_id')) $need['s'] = true;
103109

104-
if ($has('owner_first_name') || $has('owner_last_name') || $has('owner_name') || $has('owner_id')) {
110+
if ($has('owner_first_name') || $has('owner_last_name') || $has('owner_name') || $has('owner_id') || $has('member_id')) {
105111
$need['a'] = true;
106-
if ($has('owner_name')) $need['m'] = true;
112+
if($has('owner_first_name') || $has('owner_last_name') || $has('owner_name') || $has('member_id')) $need['m'] = true;
107113
}
108114

109115
if ($has('owner_email')) {
@@ -117,11 +123,18 @@ private function requiredAliases(?Filter $filter, ?Order $order): array
117123
$need['a'] = true;
118124
}
119125

126+
if ($has('owner_status')) {
127+
$this->joinCatalog['a'][1] = 'join';
128+
$need['a'] = true;
129+
}
130+
120131
if ($has('has_order_owner')) {
121132
if ((string)$val('has_order_owner') === '1') $this->joinCatalog['ord_m'][1] = 'join';
122133
$need['o'] = $need['ord_m'] = true;
123134
}
124135

136+
if ($has('assigned_to')) { $need['a'] = true; $need['m'] = true; } // usa m.id y a.email
137+
125138
if ($has('promo_code') || $has('promo_code_id') || $has('promo_code_description')) {
126139
$need['pc'] = true;
127140
}
@@ -160,7 +173,7 @@ private function requiredAliases(?Filter $filter, ?Order $order): array
160173
$need['b'] = $need['bt'] = $need['avt'] = true;
161174
}
162175

163-
// --- Order ---
176+
// --- Orders ---
164177
if ($ord('owner_first_name') || $ord('owner_last_name') || $ord('owner_name')) {
165178
$need['a'] = $need['m'] = true;
166179
}
@@ -214,6 +227,7 @@ protected function applyExtraSelects(QueryBuilder $query, ?Filter $filter = null
214227

215228
return $query;
216229
}
230+
217231
/**
218232
* @return array
219233
*/
@@ -223,6 +237,7 @@ protected function getFilterMappings()
223237
$filter = count($args) > 0 ? $args[0] : null;
224238
$owner_member_id = 0;
225239
$owner_member_email = null;
240+
226241
if($filter instanceof Filter) {
227242
if ($filter->hasFilter("owner_member_id")) {
228243
$owner_member_id = $filter->getValue("owner_member_id")[0];
@@ -420,15 +435,12 @@ protected function applyExtraJoins(QueryBuilder $query, ?Filter $filter = null,
420435
$this->joinCatalog['a'][1] = 'leftJoin';
421436
$this->joinCatalog['bt'][1] = 'leftJoin';
422437
$this->joinCatalog['al'][1] = 'leftJoin';
438+
$this->joinCatalog['ord_m'][1] = 'leftJoin';
423439

424440
foreach ($this->requiredAliases($filter, $order) as $alias) {
425441
$this->ensureJoin($query, $alias);
426442
}
427443

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-
}
432444

433445
return $query;
434446
}
@@ -704,4 +716,45 @@ public function getAllTicketsIdsByOrder(int $order_id, PagingInfo $paging_info):
704716
$res = $query->getQuery()->getArrayResult();
705717
return array_column($res, 'id');
706718
}
719+
720+
721+
/**
722+
* @param PagingInfo $paging_info
723+
* @param Filter|null $filter
724+
* @param Order|null $order
725+
* @return PagingResponse
726+
*/
727+
public function getAllByPage(PagingInfo $paging_info, Filter $filter = null, Order $order = null){
728+
$start = time();
729+
Log::debug(sprintf('DoctrineSummitAttendeeTicketRepository::getAllByPage'));
730+
$total = $this->getFastCount($filter, $order);
731+
$ids = $this->getAllIdsByPage($paging_info, $filter, $order);
732+
$query = $this->getEntityManager()->createQueryBuilder()
733+
->select('e, a, o, tt, pc, b, bt, a_c, m')
734+
->from($this->getBaseEntity(), 'e')
735+
->leftJoin('e.owner', 'a')->addSelect('a')
736+
->leftJoin('e.order', 'o')->addSelect('o')
737+
->leftJoin('e.ticket_type', 'tt')->addSelect('tt')
738+
->leftJoin('e.promo_code', 'pc')->addSelect('pc')
739+
->leftJoin('e.badge', 'b')->addSelect('b')
740+
->leftJoin('b.type', 'bt')->addSelect('bt')
741+
->leftJoin('a.company', 'a_c')->addSelect('a_c')
742+
->leftJoin('a.member', 'm')->addSelect('m')
743+
->where('e.id IN (:ids)')
744+
->setParameter('ids', $ids);
745+
746+
$data = [];
747+
foreach( $query->getQuery()->getResult() as $entity)
748+
array_push($data, $entity);
749+
$end = time() - $start;
750+
Log::debug(sprintf('DoctrineSummitAttendeeTicketRepository::getAllByPage %s seconds', $end));
751+
return new PagingResponse
752+
(
753+
$total,
754+
$paging_info->getPerPage(),
755+
$paging_info->getCurrentPage(),
756+
$paging_info->getLastPage($total),
757+
$data
758+
);
759+
}
707760
}

0 commit comments

Comments
 (0)