Model.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\command;
  3. use support\think\Db;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use think\db\exception\BindParamException;
  8. use Webman\Console\Util;
  9. class Model extends Command
  10. {
  11. protected static string $defaultName = 'model:all';
  12. protected static string $defaultDescription = '统一生成Model';
  13. protected function execute(InputInterface $input, OutputInterface $output): int
  14. {
  15. $output->writeln("生成Model开始====>".getDateFull());
  16. try {
  17. $table = Db::query("SHOW TABLES");
  18. } catch (BindParamException $e) {
  19. }
  20. $connection = config("think-orm.default");
  21. $database = config("think-orm.connections.$connection.database");
  22. foreach ($table as $val) {
  23. $dir = explode("_",$val["Tables_in_{$database}"]);
  24. $dirPath = app_path("model").DIRECTORY_SEPARATOR.$dir[0];
  25. $namespace = str_replace('/', '\\', 'app/model/' . $dir[0]);
  26. $class = Util::nameToClass($val["Tables_in_{$database}"]);
  27. $file = $dirPath . DIRECTORY_SEPARATOR . "$class.php";
  28. if (file_exists($file)) { // 存在,跳过
  29. $output->writeln("已存在,跳过====>".getDateFull()."====>".$file);
  30. } else {
  31. $path = pathinfo($file, PATHINFO_DIRNAME);
  32. if (!is_dir($path)) {
  33. mkdir($path, 0777, true);
  34. }
  35. $properties = '';
  36. $pk = 'id';
  37. foreach (Db::query("select COLUMN_NAME,DATA_TYPE,COLUMN_KEY,COLUMN_COMMENT from INFORMATION_SCHEMA.COLUMNS where table_name = '".$val["Tables_in_{$database}"]."' and table_schema = '$database' ORDER BY ordinal_position") as $item) {
  38. if ($item['COLUMN_KEY'] === 'PRI') {
  39. $pk = $item['COLUMN_NAME'];
  40. $item["COLUMN_COMMENT"] .= "(主键)";
  41. }
  42. $type = $this->getType($item["DATA_TYPE"]);
  43. $properties .= " * @property $type \${$item["COLUMN_NAME"]} {$item["COLUMN_COMMENT"]}\n";
  44. }
  45. $properties = rtrim($properties) ?: ' *';
  46. $table_val = $val["Tables_in_{$database}"];
  47. $model_content = <<<EOF
  48. <?php
  49. namespace $namespace;
  50. use app\\extra\\basic\\Model;
  51. /**
  52. $properties
  53. */
  54. class $class extends Model
  55. {
  56. /**
  57. * The connection name for the model.
  58. *
  59. * @var string|null
  60. */
  61. protected \$connection = 'mysql';
  62. /**
  63. * The table associated with the model.
  64. *
  65. * @var string
  66. */
  67. protected string \$table = "$table_val";
  68. /**
  69. * The primary key associated with the table.
  70. *
  71. * @var string
  72. */
  73. protected string \$primaryKey = "$pk";
  74. /**
  75. * Indicates if the model should be timestamped.
  76. *
  77. * @var bool
  78. */
  79. public bool \$timestamps = false;
  80. }
  81. EOF;
  82. file_put_contents($file, $model_content);
  83. $output->writeln("生成成功====>".getDateFull()."====>".$file);
  84. }
  85. }
  86. return self::SUCCESS;
  87. }
  88. protected function getType(string $type): string
  89. {
  90. if (str_contains($type, 'int')) {
  91. return 'integer';
  92. }
  93. switch ($type) {
  94. case 'varchar':
  95. case 'string':
  96. case 'text':
  97. case 'date':
  98. case 'time':
  99. case 'guid':
  100. case 'datetimetz':
  101. case 'datetime':
  102. case 'decimal':
  103. case 'enum':
  104. return 'string';
  105. case 'boolean':
  106. return 'integer';
  107. case 'float':
  108. return 'float';
  109. default:
  110. return 'mixed';
  111. }
  112. }
  113. }