diff --git a/src/DataService.php b/src/DataService.php index 6d89a9d..c5b49ef 100644 --- a/src/DataService.php +++ b/src/DataService.php @@ -37,24 +37,18 @@ class DataService $whereSql = $whereParts ? 'WHERE ' . implode(' AND ', $whereParts) : ''; $orderSql = ''; - // Логируем в файл - $logFile = __DIR__ . '/../debug.log'; - file_put_contents($logFile, date('Y-m-d H:i:s') . " === SORT DEBUG ===\n", FILE_APPEND); - file_put_contents($logFile, "Sort param: " . json_encode($sort) . "\n", FILE_APPEND); - file_put_contents($logFile, "Column names count: " . count($colNames) . "\n", FILE_APPEND); - if ($sort && !empty($sort['field'])) { - $fieldExists = in_array($sort['field'], $colNames, true); - file_put_contents($logFile, "Field '{$sort['field']}' exists: " . ($fieldExists ? 'YES' : 'NO') . "\n", FILE_APPEND); - if ($fieldExists) { - $dir = strtoupper($sort['dir'] ?? 'ASC'); - if (!in_array($dir, ['ASC', 'DESC'], true)) { - $dir = 'ASC'; - } - $orderSql = "ORDER BY `{$sort['field']}` $dir"; - file_put_contents($logFile, "ORDER SQL: $orderSql\n", FILE_APPEND); + + // ✅ Если sort пришёл как массив [{"field":..., "dir":...}], берём первый элемент + if ($sort && is_array($sort) && isset($sort[0])) { + $sort = $sort[0]; + } + + if ($sort && !empty($sort['field']) && in_array($sort['field'], $colNames, true)) { + $dir = strtoupper($sort['dir'] ?? 'ASC'); + if (!in_array($dir, ['ASC', 'DESC'], true)) { + $dir = 'ASC'; } - } else { - file_put_contents($logFile, "No sort (sort is null or field empty)\n", FILE_APPEND); + $orderSql = "ORDER BY `{$sort['field']}` $dir"; } $sql = "SELECT $selectList @@ -697,6 +691,12 @@ private function formatRowData(array $rowData): string $whereSql = $whereParts ? 'WHERE ' . implode(' AND ', $whereParts) : ''; $orderSql = ''; + + // ✅ Если sort пришёл как массив [{"field":..., "dir":...}], берём первый элемент + if ($sort && is_array($sort) && isset($sort[0])) { + $sort = $sort[0]; + } + if ($sort && !empty($sort['field']) && in_array($sort['field'], $colNames, true)) { $dir = strtoupper($sort['dir'] ?? 'ASC'); if (!in_array($dir, ['ASC', 'DESC'], true)) {