>[info] 终端命令直接继承于 ThinkPHP5 官方,所以该工具不仅仅支持 tpadmin 提供的独有功能,还有 ThinkPHP5 自带的命令行功能,其他 ThinkPHP5 的命令行功能请参考 [ThinkPHP5 - 命令行](http://www.kancloud.cn/manual/thinkphp5/122951)
## 开始使用
1. 在默认配置文件里(项目根目录下的 `generate.php` 文件里)填写需要的配置信息,格式请参考示例文件


2. 打开终端,切换目录到项目根目录
3. 执行 `php tpadmin -l` ,查看所有命令,找到代码生成器的方法为 `generate`

4. 执行 `php tpadmin generate -h` ,查看代码自动生成器的使用方法(全英文,可以复制说明翻译)

5. 执行 `php tpadmin generate` ,会使用默认配置(即配置文件默认为 `generate.php`,生成文件为 `all`)生成文件

根目录下的 `generate.php`
```
return [
'module' => 'admin',
'controller' => 'Test',
'title'=> '测试',
'form' => [
[
'title'=> '字段一',
'name' => 'field1',
'type' => 'radio',
'option' => '1:值一#2:值二#3:值三',
'default' => '默认值',
'sort' => false,
'search' => true,
'search_type' => 'select',
'require' => true,
'validate' => [
'datatype' => '*',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
],
],
[
'title'=> '字段一',
'name' => 'field2',
'type' => 'date',
'option' => '1:值一#2:值二#3:值三',
'default' => '2',
'sort' => true,
'search' => true,
'search_type' => 'text',
'require' => true,
'validate' => [
'datatype' => 'n',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
],
],
[
'title'=> '状态',
'name' => 'status',
'type' => 'radio',
'option' => '1:启用#0:禁用',
'default' => '0',
'sort' => false,
'search' => false,
'search_type' => 'select',
'require' => true,
'validate' => [
'datatype' => 'n',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
],
],
],
'create_table'=> true,
'create_table_force' => false,
'table_name' => '',
'table_engine'=> 'InnoDB',
'field'=> [
[
'name' => 'field1',
'type' => 'varchar(25)',
'default' => 123,
'not_null' => true,
'key' => true,
'comment' => '',
'extra' => '', // 扩展属性,例如AUTO_INCREMENT
],
[
'name'=> 'field2',
'type'=> 'varchar(255)',
'default' => 123,
'allow_null' => true,
'key' => true,
'comment' => '',
'extra' => '', // 扩展属性,例如AUTO_INCREMENT
],
],
'menu' => ['add', 'forbid', 'resume', 'delete', 'recyclebin'],
'auto_timestamp' => true,
'model'=> false,
'validate' => false,
];
```
>[info] 以下代码、日志全部是自动生成,没有做任何修改,包括缩进,严格的缩进非常方便二次编辑和查阅
## 生成文件
### controller/Test.php
```
namespace appadmincontroller;
thinkLoader::import('controller/Controller', thinkConfig::get('traits_path') , EXT);
use appadminController;
class Test extends Controller
{
use appadmintraitscontrollerController;
// 方法黑名单
protected static $blacklist = [];
protected function filter(&$map)
{
if ($this->request->param("field2")) {
$map['field2'] = ["like", "%" . $this->request->param("field2") . "%"];
}
}
}
```
### view/test/index.html
```
{extend name="template/base" /}
{block name="content"}
{include file="form" /}
{tp:menu menu="add,forbid,resume,delete,recyclebin" /}
共有数据 :{$count ?? '0'} 条
| 操作 |
|---|
{$vo.status|show_status=$vo.id} {tp:menu menu='sedit' /} {tp:menu menu='sdelete' /} |
{$page ?? ''}
{/block}
{block name="script"}
{/block}
```
### view/test/recyclebin.html
```
{extend name="template/recyclebin" /}
{block name="script"}
{/block}
```
### view/test/th.html
```
```
### view/test/td.html
```
```
### view/test/form.html
```
```
### view/test/edit.html
```
{extend name="template/base" /}
{block name="content"}
{/block}
{block name="script"}
{/block}
```
### view/test/config.php
```
return array (
'module' => 'admin',
'menu' =>
array (
0 => 'add',
1 => 'forbid',
2 => 'resume',
3 => 'delete',
4 => 'recyclebin',
),
'create_config' => true,
'controller' => 'Test',
'title' => '测试',
'form' =>
array (
0 =>
array (
'title' => '字段一',
'name' => 'field1',
'type' => 'radio',
'option' => '1:值一#2:值二#3:值三',
'default' => '默认值',
'sort' => false,
'search' => true,
'search_type' => 'select',
'require' => true,
'validate' =>
array (
'datatype' => '*',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
),
),
1 =>
array (
'title' => '字段一',
'name' => 'field2',
'type' => 'date',
'option' => '1:值一#2:值二#3:值三',
'default' => '2',
'sort' => true,
'search' => true,
'search_type' => 'text',
'require' => true,
'validate' =>
array (
'datatype' => 'n',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
),
),
2 =>
array (
'title' => '状态',
'name' => 'status',
'type' => 'radio',
'option' => '1:启用#0:禁用',
'default' => '0',
'sort' => false,
'search' => false,
'search_type' => 'select',
'require' => true,
'validate' =>
array (
'datatype' => 'n',
'nullmsg' => '为空信息',
'errormsg' => '错误信息',
),
),
),
'create_table' => true,
'create_table_force' => false,
'table_name' => '',
'table_engine' => 'InnoDB',
'field' =>
array (
0 =>
array (
'name' => 'field1',
'type' => 'varchar(25)',
'default' => 123,
'not_null' => true,
'key' => true,
'comment' => '',
'extra' => '',
),
1 =>
array (
'name' => 'field2',
'type' => 'varchar(255)',
'default' => 123,
'allow_null' => true,
'key' => true,
'comment' => '',
'extra' => '',
),
),
'auto_timestamp' => true,
'model' => false,
'validate' => false,
);
```
## 生成数据表
下面是 Linux 版 Navicat 截图

下面是从 Linux 版 Navicat 中拷贝出的建表语句
```
CREATE TABLE `tp_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试主键',
`field1` varchar(25) NOT NULL DEFAULT '123',
`field2` varchar(255) DEFAULT '123',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用',
`isdelete` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '删除状态,1-删除 | 0-正常',
`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `field1` (`field1`),
KEY `field2` (`field2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='测试';
```
## 生成日志
`runtime/log` 目录下的日志原文:
>[info] 在当天的日志中搜索 build_sql 即可搜索到,日志原文中的标记是 BUILD_SQL
```
[ 2016-11-10T21:29:10+08:00 ] 0.0.0.0 0.0.0.0 CLI
[ log ] cmd:tpadmin generate [运行时间:0.044912s][吞吐率:22.27req/s] [内存消耗:3,114.72kb] [文件加载:60]
[ info ] [ DB ] INIT mysql
[ info ] [ LOG ] INIT File
[ sql ] [ DB ] CONNECT:[ UseTime:0.001210s ] mysql:dbname=tpadmin;host=127.0.0.1;charset=utf8
[ sql ] [ SQL ] SHOW TABLES LIKE 'tp_test' [ RunTime:0.000311s ]
[ sql ] [ SQL ] DROP TABLE IF EXISTS `tp_test` [ RunTime:0.000150s ]
[ sql ] [ SQL ] CREATE TABLE `tp_test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '测试主键',
`field1` varchar(25) NOT NULL DEFAULT '123',
`field2` varchar(255) DEFAULT '123',
`status` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '状态,1-正常 | 0-禁用',
`isdelete` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '删除状态,1-删除 | 0-正常',
`create_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`update_time` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `field1` (`field1`),
KEY `field2` (`field2`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '测试' [ RunTime:0.023072s ]
[ sql ] [ SQL ] DROP TABLE IF EXISTS `tp_test_build_bak` [ RunTime:0.000250s ]
---------------------------------------------------------------
```



