如何快速生成数据库字典(thinkphp5.0)
本教程将教你快速生成数据库字典
示例代码使用PHP框架:Thinkphp5.0
PHP代码:
/**
* 生成数据库字典html
* 可直接另存为再copy到word文档中使用
*
* @return mixed
*/
public function dataDictionary()
{
$tables = Db::query('SHOW TABLE STATUS');
$table_list = array_map('array_change_key_case', $tables);
$table_data = [];
foreach ($table_list as $item) {
$table_name = str_replace(config('database.prefix'),'',$item['name']);
$table_fields = $this->showColumns($table_name);
foreach ($table_fields as &$fieldItem) {
$fieldItem['comment'] = $this->getDbColumnComment($table_name, $fieldItem['name']);
}
$table_data[] = [
'table_name' => $item['name'],
'table_comment' => $item['comment'],
'table_fields' => $table_fields
];
}
$this->assign('table_data', $table_data); return $this->fetch('');
} /**
* 显示表结构信息
*
* @param string $table
* @return array
*/
private function showColumns($table){ $sql = 'SHOW COLUMNS FROM `'.config('database.prefix').$table.'`';
$result = Db::query($sql);
if ($result === false) return array();
$array = array();
if (!empty($result)) {
foreach ($result as $k=>$v) {
$array[$v['Field']] = [
'name' => $v['Field'],
'type' => $v['Type'],
'null' => $v['Null'],
'default' => $v['Default'],
'primary' => (strtolower($v['Key']) == 'pri'),
'autoinc' => (strtolower($v['Extra']) == 'auto_increment'),
];
}
} return $array;
} /**
* 获取数据库字段注释
*
* @param string $table_name 数据表名称(必须,不含前缀)
* @param string|boolean $field 字段名称(默认获取全部字段,单个字段请输入字段名称)
* @param string $table_schema 数据库名称(可选)
* @return string
*/
private function getDbColumnComment($table_name = '', $field = true, $table_schema = ''){
// 接收参数
$database = config('database'); $table_schema = empty($table_schema) ? $database['database'] : $table_schema;
$table_name = $database['prefix'] . $table_name; // 处理参数
$param = [
$table_name,
$table_schema
]; // 字段
$columnName = '';
if($field !== true){
$param[] = $field;
$columnName = "AND COLUMN_NAME = ?";
} // 查询结果
$result = Db::query("SELECT COLUMN_NAME as field,column_comment as comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? AND table_schema = ? $columnName", $param);
if(empty($result) && $field !== true){
return $table_name . '表' . $field . '字段不存在';
} // 处理结果
foreach($result as $k => $v){
$data[$v['field']] = $v['comment'];
if(strpos($v['comment'], '#*#') !== false){
$tmpArr = explode('#*#', $v['comment']);
$data[$v['field']] = json_decode(end($tmpArr), true);
}
}
// 字段注释格式不正确
if(empty($data)){
return $table_name . '表' . $field . '字段注释格式不正确';
}
return count($data) == 1 ? reset($data) : $data;
}
Html代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>快速生成数据库字典</title>
<style type="text/css">
.table-name {
text-align: center;
margin: 15px auto;
font-weight: bold;
font-size: 20px;
} table {
border-collapse: collapse;
margin: 0 auto;
text-align: center;
width: 550px;
} table caption {
margin: 15px auto;
} table td, table th {
border: 1px solid #cad9ea;
color: #666;
height: 30px;
} table thead th {
background-color: #CCE8EB;
width: 100px;
} table tr:nth-child(odd) {
background: #fff;
} table tr:nth-child(even) {
background: #F5FAFA;
}
</style>
</head>
<body> {foreach $table_data as $tableInfo}
<div class="table-name">{$tableInfo.table_name}(表注释:{$tableInfo.table_comment})</div>
<table class="table">
<thead>
<tr>
<th>字段</th>
<th>类型</th>
<th>空</th>
<th>默认</th>
<th>注释</th>
</tr>
</thead>
<tbody>
{foreach $tableInfo['table_fields'] as $field}
<tr>
<td>
{$field.name}
</td>
<td>
{$field.type}
</td>
<td>
{$field.null}
</td>
<td style="max-width: 30px;">
{$field.default}
</td>
<td>
{$field.comment}
</td>
</tr>
{/foreach}
</tbody>
</table>
{/foreach} </body>
</html>
注:如需保存到word文档,需网页另存为html文件并用word软件打开,将内容全选复制到新的word文档中即可。
如有不明白的,欢迎下方留言。
如何快速生成数据库字典(thinkphp5.0)的更多相关文章
- 使用Navicat快速生成数据库字典
https://blog.csdn.net/maquealone/article/details/60764420
- 自动生成数据库字典(sql2008)
每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 存储过程: SET ANSI_NULLS ON GO SET QUOT ...
- 自动生成数据库字典(sql2008) 转自 飘渺の云海
每次做项目的时候都要做数据字典,这种重复的工作实在很是痛苦,于是广找资料,终于完成了自动生成数据库字典的工作,废话少说,上代码. 截取一部分图片: 存储过程: SET ANSI_NULLS ON GO ...
- MS SQL生成数据库字典脚本
开发一个项目时都会有一个蛋疼的问题——写数据库需求文档,然后根据这个文档来建数据库,如果后来需求改了,要改数据库还要改文档,有时忙着忙着就忘改了,导致文档是过期的.那么我们自己写个脚本在数据库运行直接 ...
- SQL Server2008生成数据库字典
1.我们在开发过程中可能会遇到这样的一种情况"当我们进行维护其他人的项目时或者项目的二次开发时可能会对原始的数据表进行分析",这里为大家介绍一种方便快捷生成数据库字典的方式. 我们 ...
- 【EF框架】EF DBFirst 快速生成数据库实体类 Database1.tt
现有如下需求,数据库表快速映射到数据库实体类 VS给出的两个选择都有问题,并不能实现,都是坑啊 EF .x DbContext 生成器 EF .x DbContext 生成器 测试结果如下 生成文件 ...
- IDEA快速生成数据库表的实体类
IDEA连接数据库 IDEA右边侧栏有个DataSource,可以通过这个来连接数据库,我们先成功连接数据库 点击进入后填写数据库进行连接,注意记得一定要去Test Connection 确保正常连接 ...
- DataDictionaryTool 一款生成数据库字典工具支持mysql和oracle
因为常常查看mysql数据结构,频繁操作.很不爽,于是想把数据表制作成数据字典,于是网上搜的一款工具 DataDictionaryTool ,最终制作成功,分享给大家! 1,此工具需要安装jre ,简 ...
- PowerDesigner 生成数据库字典(有图有真相,绝对自创非转载)
最近用pd做模型,生成数据字典时在网上找了很多,但是看的都很晕,说的不明白. 经过自己研究终于找到一个简单的方式,当然这只是简单的,大家举一反三去吧.辛苦弄的,求点赞!!! 先看效果图: 现在说一下步 ...
随机推荐
- python 数据结构应用
- dubbo学习总结一 API
API 一般用来暴露接口 项目分层一般是 api + entity + enums + model 就是接口加上一些实体之类的东西
- 用两个栈实现队列(C++ 和 Python 实现)
(说明:本博客中的题目.题目详细说明及参考代码均摘自 “何海涛<剑指Offer:名企面试官精讲典型编程题>2012年”) 题目 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数 a ...
- png的故事:隔行扫描算法
转载自AlloyTeam:http://www.alloyteam.com/2017/06/the-story-of-png-deinterlacing-algorithm/ 前言 前文已经讲解过如何 ...
- ZT————pull push mode
谁能讲讲push和pull模式是什么意思?(参与有分) [问题点数:100分,结帖人mickeyfirst] 收藏 mickeyfirst mickeyfirst 等级: 结帖率:94.12% 楼主 ...
- 使用Android Studio和Gradle编译NDK项目之Experimental Plugin User Guide
转载自:http://tools.android.com/tech-docs/new-build-system/gradle-experimental Introduction The new exp ...
- 发布Hessian服务作为服务内部基础服务
摘要:Hessian经常作为服务内部RPC工具来使用,速度快效率高.重构代码的核心思想就是把共用的代码段提出来,使代码结构优化:架构设计类似,把基本的共用的服务提出来,使架构优化.下面讲述一下我在具体 ...
- Cloud Tool 小探索
Google Apps不用多说. Google drive免费提供15GB的容量. Microsoft Windows Live感觉功能上和google相比无亮点和优势. SkyDrive免费提供7G ...
- UVa 10791 - Minimum Sum LCM(唯一分解定理)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CPP-基础:互斥量
互斥量的用途和临界区很像.它与临界区的差别在于可以跨线程使用,可以用来同步进行多个线程间的数据访问,但是是以牺牲速度为代价的.只有临界区是非核心对象,那么互斥量就是一个核心对象了.核心对象的特点是有所 ...