2222
2323namespace App \DataTables ;
2424
25+ use App \DataTables \Adapters \TwoStepORMAdapter ;
2526use App \DataTables \Column \EntityColumn ;
2627use App \DataTables \Column \EnumColumn ;
2728use App \DataTables \Column \LocaleDateTimeColumn ;
2829use App \DataTables \Column \MarkdownColumn ;
2930use App \DataTables \Helpers \PartDataTableHelper ;
30- use App \Entity \ Attachments \ Attachment ;
31+ use App \Doctrine \ Helpers \ FieldHelper ;
3132use App \Entity \Parts \Part ;
3233use App \Entity \Parts \ManufacturingStatus ;
3334use App \Entity \ProjectSystem \ProjectBOMEntry ;
3435use App \Services \ElementTypeNameGenerator ;
3536use App \Services \EntityURLGenerator ;
3637use App \Services \Formatters \AmountFormatter ;
38+ use Doctrine \ORM \AbstractQuery ;
3739use Doctrine \ORM \QueryBuilder ;
3840use Omines \DataTablesBundle \Adapter \Doctrine \ORM \SearchCriteriaProvider ;
39- use Omines \DataTablesBundle \Adapter \Doctrine \ORMAdapter ;
4041use Omines \DataTablesBundle \Column \TextColumn ;
4142use Omines \DataTablesBundle \DataTable ;
4243use Omines \DataTablesBundle \DataTableTypeInterface ;
@@ -152,6 +153,8 @@ public function configure(DataTable $dataTable, array $options): void
152153
153154 ->add ('manufacturing_status ' , EnumColumn::class, [
154155 'label ' => $ this ->translator ->trans ('part.table.manufacturingStatus ' ),
156+ 'data ' => static fn (ProjectBOMEntry $ context ): ?ManufacturingStatus => $ context ->getPart ()?->getManufacturingStatus(),
157+ 'orderField ' => 'part.manufacturing_status ' ,
155158 'class ' => ManufacturingStatus::class,
156159 'render ' => function (?ManufacturingStatus $ status , ProjectBOMEntry $ context ): string {
157160 if ($ status === null ) {
@@ -211,11 +214,13 @@ public function configure(DataTable $dataTable, array $options): void
211214
212215 $ dataTable ->addOrderBy ('name ' , DataTable::SORT_ASCENDING );
213216
214- $ dataTable ->createAdapter (ORMAdapter::class, [
215- 'entity ' => Attachment::class,
216- 'query ' => function (QueryBuilder $ builder ) use ($ options ): void {
217- $ this ->getQuery ($ builder , $ options );
217+ $ dataTable ->createAdapter (TwoStepORMAdapter::class, [
218+ 'entity ' => ProjectBOMEntry::class,
219+ 'hydrate ' => AbstractQuery::HYDRATE_OBJECT ,
220+ 'filter_query ' => function (QueryBuilder $ builder ) use ($ options ): void {
221+ $ this ->getFilterQuery ($ builder , $ options );
218222 },
223+ 'detail_query ' => $ this ->getDetailQuery (...),
219224 'criteria ' => [
220225 function (QueryBuilder $ builder ) use ($ options ): void {
221226 $ this ->buildCriteria ($ builder , $ options );
@@ -225,22 +230,56 @@ function (QueryBuilder $builder) use ($options): void {
225230 ]);
226231 }
227232
228- private function getQuery (QueryBuilder $ builder , array $ options ): void
233+ private function getFilterQuery (QueryBuilder $ builder , array $ options ): void
229234 {
230235 $ builder
231- ->select ('bom_entry ' )
232- ->addSelect ('part ' )
236+ ->select ('bom_entry.id ' )
233237 ->from (ProjectBOMEntry::class, 'bom_entry ' )
234238 ->leftJoin ('bom_entry.part ' , 'part ' )
235239 ->leftJoin ('part.category ' , 'category ' )
236- ->leftJoin ('part.partLots ' , 'partLots ' )
237- ->leftJoin ('partLots .storage_location ' , 'storelocations ' )
240+ ->leftJoin ('part.partLots ' , '_partLots ' )
241+ ->leftJoin ('_partLots .storage_location ' , '_storelocations ' )
238242 ->leftJoin ('part.footprint ' , 'footprint ' )
239243 ->leftJoin ('part.manufacturer ' , 'manufacturer ' )
240244 ->leftJoin ('part.partCustomState ' , 'partCustomState ' )
241245 ->where ('bom_entry.project = :project ' )
242246 ->setParameter ('project ' , $ options ['project ' ])
247+ ->addGroupBy ('bom_entry ' )
248+ ->addGroupBy ('part ' )
249+ ->addGroupBy ('category ' )
250+ ->addGroupBy ('footprint ' )
251+ ->addGroupBy ('manufacturer ' )
252+ ->addGroupBy ('partCustomState ' )
253+ ;
254+ }
255+
256+ private function getDetailQuery (QueryBuilder $ builder , array $ filter_results ): void
257+ {
258+ $ ids = array_map (static fn (array $ row ) => $ row ['id ' ], $ filter_results );
259+ if ($ ids === []) {
260+ $ ids = [-1 ];
261+ }
243262
263+ $ builder
264+ ->select ('bom_entry ' )
265+ ->addSelect ('part ' )
266+ ->addSelect ('category ' )
267+ ->addSelect ('partLots ' )
268+ ->addSelect ('storelocations ' )
269+ ->addSelect ('footprint ' )
270+ ->addSelect ('manufacturer ' )
271+ ->addSelect ('partCustomState ' )
272+ ->from (ProjectBOMEntry::class, 'bom_entry ' )
273+ ->leftJoin ('bom_entry.part ' , 'part ' )
274+ ->leftJoin ('part.category ' , 'category ' )
275+ ->leftJoin ('part.partLots ' , 'partLots ' )
276+ ->leftJoin ('partLots.storage_location ' , 'storelocations ' )
277+ ->leftJoin ('part.footprint ' , 'footprint ' )
278+ ->leftJoin ('part.manufacturer ' , 'manufacturer ' )
279+ ->leftJoin ('part.partCustomState ' , 'partCustomState ' )
280+ ->where ('bom_entry.id IN (:ids) ' )
281+ ->setParameter ('ids ' , $ ids )
282+ ->addGroupBy ('bom_entry ' )
244283 ->addGroupBy ('part ' )
245284 ->addGroupBy ('partLots ' )
246285 ->addGroupBy ('category ' )
@@ -249,6 +288,8 @@ private function getQuery(QueryBuilder $builder, array $options): void
249288 ->addGroupBy ('manufacturer ' )
250289 ->addGroupBy ('partCustomState ' )
251290 ;
291+
292+ FieldHelper::addOrderByFieldParam ($ builder , 'bom_entry.id ' , 'ids ' );
252293 }
253294
254295 private function buildCriteria (QueryBuilder $ builder , array $ options ): void
0 commit comments