diff --git a/public/index.php b/public/index.php index a5c3dda..31d3bdf 100644 --- a/public/index.php +++ b/public/index.php @@ -103,7 +103,7 @@ $app->post('/api/table/data', function (Request $request, Response $response) us $columns = $payload['columns'] ?? []; // ✅ Логирование для отладки - error_log("Data request: page=$page, pageSize=$pageSize, filters=" . json_encode($filters)); + error_log("Data request: page=$page, pageSize=$pageSize, filters=" . json_encode($filters) . ", sort=" . json_encode($sort)); $pdo = $container->get('db'); $ds = new \App\DataService($pdo); diff --git a/src/DataService.php b/src/DataService.php index 2390369..47f54d2 100644 --- a/src/DataService.php +++ b/src/DataService.php @@ -37,12 +37,22 @@ class DataService $whereSql = $whereParts ? 'WHERE ' . implode(' AND ', $whereParts) : ''; $orderSql = ''; - 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'; + error_log("=== SORT DEBUG (fetchData) ==="); + error_log("Sort param: " . json_encode($sort)); + error_log("Column names: " . implode(', ', array_slice($colNames, 0, 5)) . "..."); + if ($sort && !empty($sort['field'])) { + $fieldExists = in_array($sort['field'], $colNames, true); + error_log("Field '{$sort['field']}' exists: " . ($fieldExists ? 'YES' : 'NO')); + if ($fieldExists) { + $dir = strtoupper($sort['dir'] ?? 'ASC'); + if (!in_array($dir, ['ASC', 'DESC'], true)) { + $dir = 'ASC'; + } + $orderSql = "ORDER BY `{$sort['field']}` $dir"; + error_log("ORDER SQL: $orderSql"); } - $orderSql = "ORDER BY `{$sort['field']}` $dir"; + } else { + error_log("No sort applied"); } $sql = "SELECT $selectList @@ -50,6 +60,7 @@ class DataService $whereSql $orderSql LIMIT :limit OFFSET :offset"; + error_log("Final SQL: " . preg_replace('/\s+/', ' ', $sql)); $stmt = $this->pdo->prepare($sql); foreach ($params as $k => $v) {