AdminIndexController.php
6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* 自动生成数据词典
* User: rexsea
* Date: 2017-10-19
* Time: 9:05
*/
namespace plugins\rexsea_dictionary\controller;
use cmf\controller\PluginBaseController;
use think\Db;
class AdminIndexController extends PluginBaseController
{
public function index()
{
$dictionary = [];
$tables = Db::query("show tables");
!$tables && $this->error("获取数据表数据失败!");
// 取得所有的表名
foreach( $tables as $k=>$v)
{
$dictionary [] ['TABLE_NAME'] = $v['Tables_in_'.config('database.database')];
}
foreach ( $dictionary as $k => $v ) {
$comment = Db::query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '".$v['TABLE_NAME']."' AND table_schema = '".config('database.database')."'");
foreach($comment as $kc=>$vc)
{
$dictionary [$k] ['TABLE_COMMENT'] = $vc['TABLE_COMMENT'];
}
$column = Db::query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$v['TABLE_NAME']."' AND table_schema = '".config('database.database')."'");
$fields = array ();
foreach($column as $kn=>$vn)
{
$fields [] = $vn;
}
$dictionary [$k] ['COLUMN'] = $fields;
}
//HTML
$html = '';
foreach($dictionary as $k=>$v)
{
$html .= "<h4>{$v['TABLE_NAME']} {$v['TABLE_COMMENT']}</h4>";
$html .= "<table class=\"table table-bordered\"><thead><tr><th>字段名</th><th>数据类型</th><th>默认值</th><th>允许非空</th><th>自动递增</th><th>备注</th></tr></thead><tbody>";
foreach($v['COLUMN'] as $vc)
{
$html .= "<tr>";
$html .= "<td>".(isset($vc['COLUMN_NAME'])?$vc['COLUMN_NAME']:"")."</td>";
$html .= "<td>".(isset($vc['COLUMN_TYPE'])?$vc['COLUMN_TYPE']:"")."</td>";
$html .= "<td>".(isset($vc['COLUMN_DEFAULT'])?$vc['COLUMN_DEFAULT']:"")."</td>";
$html .= "<td>".(isset($vc['IS_NULLABLE'])?$vc['IS_NULLABLE']:"")."</td>";
$html .= "<td>".(isset($vc['EXTRA'])?$vc['EXTRA']:"")."</td>";
$html .= "<td>".(isset($vc['COLUMN_COMMENT'])?$vc['COLUMN_COMMENT']:"")."</td>";
$html .= "</tr>";
}
$html .= "</tbody></table>";
}
$this->assign('html',$html);
return $this->fetch('/admin_index');
}
public function down()
{
$dictionary = [];
$tables = Db::query("show tables");
!$tables && $this->error("获取数据表数据失败!");
// 取得所有的表名
foreach( $tables as $k=>$v)
{
$dictionary [] ['TABLE_NAME'] = $v['Tables_in_'.config('database.database')];
}
foreach ( $dictionary as $k => $v ) {
$comment = Db::query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE table_name = '".$v['TABLE_NAME']."' AND table_schema = '".config('database.database')."'");
foreach($comment as $kc=>$vc)
{
$dictionary [$k] ['TABLE_COMMENT'] = $vc['TABLE_COMMENT'];
}
$column = Db::query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '".$v['TABLE_NAME']."' AND table_schema = '".config('database.database')."'");
$fields = array ();
foreach($column as $kn=>$vn)
{
$fields [] = $vn;
}
$dictionary [$k] ['COLUMN'] = $fields;
}
//HTML
$html = '';
foreach($dictionary as $k=>$v)
{
$html .= "<h4>{$v['TABLE_NAME']} {$v['TABLE_COMMENT']}</h4>";
$html .= "<table style='border:solid #add9c0; border-width:1px 0px 0px 1px;' class=\"table table-bordered\"><thead><tr><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>字段名</th><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>数据类型</th><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>默认值</th><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>允许非空</th><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>自动递增</th><th style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>备注</th></tr></thead><tbody>";
foreach($v['COLUMN'] as $vc)
{
$html .= "<tr>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['COLUMN_NAME'])?$vc['COLUMN_NAME']:"")."</td>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['COLUMN_TYPE'])?$vc['COLUMN_TYPE']:"")."</td>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['COLUMN_DEFAULT'])?$vc['COLUMN_DEFAULT']:"")."</td>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['IS_NULLABLE'])?$vc['IS_NULLABLE']:"")."</td>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['EXTRA'])?$vc['EXTRA']:"")."</td>";
$html .= "<td style='border:solid #add9c0; border-width:0px 1px 1px 0px; padding:10px 0px;'>".(isset($vc['COLUMN_COMMENT'])?$vc['COLUMN_COMMENT']:"")."</td>";
$html .= "</tr>";
}
$html .= "</tbody></table>";
}
$this->start();
$wordname=config('database.database').'.doc';
echo $html;
@header('Content-type:application/word');
header("Content-Disposition: attachment; filename=$wordname");
@readfile($wordname);
ob_flush();//每次执行前刷新缓存
flush();
}
protected function start(){
ob_start();
echo '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<xml><w:WordDocument><w:View>Print</w:View></xml>
</head><body>';
}
}