Skip to content

Commit b5581e6

Browse files
Copilotjbtronics
andcommitted
Implement DTO-based PartsDataTable with optimized queries
Co-authored-by: jbtronics <5410681+jbtronics@users.noreply.github.com>
1 parent 0aeae79 commit b5581e6

6 files changed

Lines changed: 859 additions & 69 deletions

File tree

src/DataTables/Adapters/TwoStepORMAdapter.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class TwoStepORMAdapter extends ORMAdapter
5454

5555
private \Closure|null $query_modifier = null;
5656

57+
private \Closure|null $dto_hydrator = null;
58+
5759
public function __construct(?ManagerRegistry $registry = null)
5860
{
5961
parent::__construct($registry);
@@ -82,6 +84,10 @@ protected function configureOptions(OptionsResolver $resolver): void
8284
$resolver->setDefault('query_modifier', null);
8385
$resolver->setAllowedTypes('query_modifier', ['null', \Closure::class]);
8486

87+
//Add the possibility to use a custom DTO hydrator instead of entity hydration
88+
$resolver->setDefault('dto_hydrator', null);
89+
$resolver->setAllowedTypes('dto_hydrator', ['null', \Closure::class]);
90+
8591
}
8692

8793
protected function afterConfiguration(array $options): void
@@ -90,6 +96,7 @@ protected function afterConfiguration(array $options): void
9096
$this->detailQueryCallable = $options['detail_query'];
9197
$this->use_simple_total = $options['simple_total_query'];
9298
$this->query_modifier = $options['query_modifier'];
99+
$this->dto_hydrator = $options['dto_hydrator'];
93100
}
94101

95102
protected function prepareQuery(AdapterQuery $query): void
@@ -189,9 +196,19 @@ protected function getResults(AdapterQuery $query): \Traversable
189196

190197
$detail_query = $detail_qb->getQuery();
191198

192-
//We pass the results of the detail query to the datatable for view rendering
193-
foreach ($detail_query->getResult() as $item) {
194-
yield $item;
199+
// If a DTO hydrator is configured, use array hydration and build DTOs
200+
if ($this->dto_hydrator !== null) {
201+
$arrayResults = $detail_query->getArrayResult();
202+
$dtos = $this->dto_hydrator->__invoke($arrayResults);
203+
foreach ($dtos as $dto) {
204+
yield $dto;
205+
}
206+
} else {
207+
// Original behavior: hydrate as entities
208+
//We pass the results of the detail query to the datatable for view rendering
209+
foreach ($detail_query->getResult() as $item) {
210+
yield $item;
211+
}
195212
}
196213
}
197214

0 commit comments

Comments
 (0)