common.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. use ba\Filesystem;
  3. use think\facade\Db;
  4. if (!function_exists('get_controller_list')) {
  5. function get_controller_list($app = 'admin'): array
  6. {
  7. $controllerDir = root_path() . 'app' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR . 'controller' . DIRECTORY_SEPARATOR;
  8. return Filesystem::getDirFiles($controllerDir);
  9. }
  10. }
  11. if (!function_exists('get_table_list')) {
  12. function get_table_list(): array
  13. {
  14. $tableList = [];
  15. $database = config('database.connections.mysql.database');
  16. $tables = Db::query("SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema = ? ", [$database]);
  17. foreach ($tables as $row) {
  18. $tableList[$row['TABLE_NAME']] = $row['TABLE_NAME'] . ($row['TABLE_COMMENT'] ? ' - ' . $row['TABLE_COMMENT'] : '');
  19. }
  20. return $tableList;
  21. }
  22. }
  23. if (!function_exists('get_table_fields')) {
  24. function get_table_fields($table, $onlyCleanComment = false): array
  25. {
  26. if (!$table) return [];
  27. $dbname = config('database.connections.mysql.database');
  28. $prefix = config('database.connections.mysql.prefix');
  29. // 从数据库中获取表字段信息
  30. $sql = "SELECT * FROM `information_schema`.`columns` "
  31. . "WHERE TABLE_SCHEMA = ? AND table_name = ? "
  32. . "ORDER BY ORDINAL_POSITION";
  33. $columnList = Db::query($sql, [$dbname, $table]);
  34. if (!$columnList) {
  35. $columnList = Db::query($sql, [$dbname, $prefix . $table]);
  36. }
  37. $fieldList = [];
  38. foreach ($columnList as $item) {
  39. if ($onlyCleanComment) {
  40. $fieldList[$item['COLUMN_NAME']] = '';
  41. if ($item['COLUMN_COMMENT']) {
  42. $comment = explode(':', $item['COLUMN_COMMENT']);
  43. $fieldList[$item['COLUMN_NAME']] = $comment[0];
  44. }
  45. continue;
  46. }
  47. $fieldList[$item['COLUMN_NAME']] = $item;
  48. }
  49. return $fieldList;
  50. }
  51. }