TableSchemaController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace console\controllers;
  3. use common\components\dirUtil;
  4. use Yii;
  5. use yii\console\Controller;
  6. class TableSchemaController extends Controller
  7. {
  8. //生成数据库字典表 ssh
  9. //执行 ./yii table-schema/create all 生成table.md文件到根目录的doc目录下
  10. //使用谷歌浏览器打开即可
  11. public function actionCreate()
  12. {
  13. global $argv;
  14. if (!$argv[2] || strcasecmp($argv[2], 'help') === 0) {
  15. echo "Usage: ./yii table-schema/create [all|tablename] [filename]\n";
  16. exit;
  17. }
  18. $db = Yii::$app->db;
  19. $allTables = $db->getSchema()->getTableNames();
  20. if ('all' === $argv[2]) {
  21. $tables = array_diff($allTables, $this->filterTables());
  22. } else {
  23. if (!in_array($argv[2], $allTables)) {
  24. echo sprintf("%s isn't exist \n", $argv[2]);
  25. exit;
  26. }
  27. $tables = (array)$argv[2];
  28. }
  29. // 当前数据库没有表
  30. if (count(array_filter($tables)) == 0) {
  31. echo "Database has not table \n";
  32. }
  33. $filePath = dirUtil::getRootDir() . '/doc/table.md';
  34. $fp = fopen($filePath, 'a+');
  35. if (!$fp) {
  36. echo "Open file failed \n";
  37. }
  38. foreach ($tables as $table) {
  39. $schema = $db->getTableSchema($table, true);
  40. if (!$schema->columns) {
  41. continue;
  42. }
  43. fwrite($fp, "#### $schema->name 表 \n");
  44. // 表头
  45. $header = "| 字段名 | 类型 | 说明 | \n";
  46. $header .= "|:--------:|:---------:|:-------:| \n";
  47. fwrite($fp, $header);
  48. // 字段
  49. $row = '';
  50. foreach ($schema->columns as $col => $obj) {
  51. $comment = $obj->isPrimaryKey ? '主键' : $obj->comment;
  52. $row .= "| $obj->name | $obj->dbType | $comment | \n";
  53. }
  54. fwrite($fp, $row);
  55. fwrite($fp, "\r\n\r\n");
  56. echo "$schema->name successfully \n";
  57. }
  58. fclose($fp);
  59. }
  60. /**
  61. * 需要过滤的表(不希望生成文档的表)
  62. * @return array
  63. */
  64. protected function filterTables()
  65. {
  66. $filterTables = [
  67. 'tbmigration',
  68. 'tbAuthAssignment',
  69. 'tbAuthItemChild',
  70. 'tbAuthRule',
  71. 'tbItemTable'
  72. ];
  73. return $filterTables;
  74. }
  75. /**
  76. * 所有表
  77. * @return \string[]
  78. */
  79. protected function allTables()
  80. {
  81. return Yii::$app->db->getSchema()->getTableNames();
  82. }
  83. /**
  84. * 清空
  85. */
  86. public function actionClear()
  87. {
  88. global $argv;
  89. if (!$argv[2] || strcasecmp($argv[2], 'help') === 0) {
  90. echo "Usage: ./yii table-schema/clear [filename]\n";
  91. exit;
  92. }
  93. $filePath = dirUtil::getRootDir() . '/doc/table.md';
  94. if (!is_file($filePath)) {
  95. echo "$filePath isn't exists \n";
  96. exit;
  97. }
  98. $fp = fopen($filePath, 'w');
  99. if (!$fp) {
  100. echo "Open file failed \n";
  101. }
  102. fwrite($fp, '');
  103. fclose($fp);
  104. echo "Clear successfully \n";
  105. }
  106. }