Skip to content

Commit 4da2bd0

Browse files
committed
fix: ordinamento tabelle
1 parent 5b7a63a commit 4da2bd0

1 file changed

Lines changed: 32 additions & 2 deletions

File tree

src/Util/Query.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ public static function getQuery($structure, $search = [], $order = [], $limit =
265265

266266
// Applica l'ordinamento solo se abbiamo una clausola valida
267267
if (!empty($final_order_clause)) {
268-
// Rimozione ORDER BY esistente con regex più robusta
269-
$query = preg_replace('/\s+ORDER\s+BY\s+[^)]*$/is', '', (string) $query);
268+
// Rimozione completa dell'ORDER BY esistente dalla query
269+
// Se è impostato un ordinamento manuale, rimuove completamente l'ORDER BY definito dalla query
270+
$query = self::removeExistingOrderBy($query);
270271
$query .= ' ORDER BY '.$final_order_clause.' '.$direction;
271272
}
272273
}
@@ -594,6 +595,35 @@ protected static function buildIdFilter($field, $original_value)
594595
return null;
595596
}
596597

598+
/**
599+
* Rimuove completamente la clausola ORDER BY esistente dalla query.
600+
* Quando viene impostato un ordinamento manuale, questo metodo rimuove
601+
* l'ORDER BY definito dalla query originale per applicare quello manuale.
602+
*
603+
* @param string $query
604+
*
605+
* @return string
606+
*/
607+
protected static function removeExistingOrderBy($query)
608+
{
609+
// Rimozione più robusta dell'ORDER BY esistente
610+
// Gestisce diversi casi: ORDER BY alla fine, ORDER BY seguito da LIMIT, etc.
611+
612+
// Pattern per trovare ORDER BY e tutto quello che segue fino alla fine o fino a LIMIT
613+
$patterns = [
614+
// ORDER BY seguito da LIMIT
615+
'/\s+ORDER\s+BY\s+[^L]*?(?=\s+LIMIT\s+)/is',
616+
// ORDER BY alla fine della query
617+
'/\s+ORDER\s+BY\s+.*$/is',
618+
];
619+
620+
foreach ($patterns as $pattern) {
621+
$query = preg_replace($pattern, '', $query);
622+
}
623+
624+
return trim($query);
625+
}
626+
597627
/**
598628
* Sostituisce la prima occorenza di una determinata stringa.
599629
*

0 commit comments

Comments
 (0)