perf(insurance): batch-fetch service items in coverage list (H8)
listServiceCoverage called serviceItemRepo->find() once per coverage row (N+1). Collect the ids and fetch them in one findBy(['id' => $ids]), then map by id. Regression: tests/Insurance/ServiceCoverageNPlusOneTest (functional correctness — every row resolves the right service_item_uuid). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -405,10 +405,19 @@ class InsuranceController extends BaseController
|
||||
|
||||
$rows = $this->serviceCoverageRepo->findByContract($contract->getId());
|
||||
|
||||
$data = array_map(function ($r) {
|
||||
// Batch-fetch the referenced service items once instead of one find()
|
||||
// per coverage row (N+1).
|
||||
$itemIds = array_values(array_unique(array_map(fn($r) => $r->getServiceItemId(), $rows)));
|
||||
$uuidById = [];
|
||||
if ($itemIds !== []) {
|
||||
foreach ($this->serviceItemRepo->findBy(['id' => $itemIds]) as $item) {
|
||||
$uuidById[$item->getId()] = $item->getUuid();
|
||||
}
|
||||
}
|
||||
|
||||
$data = array_map(function ($r) use ($uuidById) {
|
||||
$row = $r->toArray();
|
||||
$item = $this->serviceItemRepo->find($r->getServiceItemId());
|
||||
$row['service_item_uuid'] = $item?->getUuid();
|
||||
$row['service_item_uuid'] = $uuidById[$r->getServiceItemId()] ?? null;
|
||||
return $row;
|
||||
}, $rows);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user