# 快速创建模型
快速创建模型 整合自 `Yii2` 的 `Gii` `Model`,需要在终端命令下操作,无WEB页面。
## 步骤
> 1. 配置好数据库连接。
>
> 2. 无须启动服务
>
> 3. 通过命令创建数据库
>
基本命令:
~~~
php server.php model --tableName=p_customer --modelClass=CustomerModel
~~~
用法和参数同 `Yii2` 大致一样。
命令可选参数:
> \--namespace 模型的命名空间,默认为:`App\Model`。
>
> \--standardizeCapitals,模型名称以驼峰命令, 默认为 true。
## 例子:
### MySQL表结构
~~~
CREATE TABLE `p_customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`wx_id` int(11) DEFAULT NULL COMMENT '微信ID',
`customer_name` varchar(180) DEFAULT NULL COMMENT '客户姓名',
`customer_username` varchar(40) DEFAULT NULL COMMENT '客户姓名',
`customer_password_hash` varchar(60) DEFAULT NULL COMMENT '密码HASH',
`created_at` int(10) DEFAULT NULL,
`updated_at` int(10) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `wx_id` (`wx_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='客户';
~~~
### 执行命令
~~~
[root@localhost esd-iot-warter-demo]# php server.php model --tableName=p_customer --modelClass=CustomerModel
________ ______ ______ ____ ____ _____ _____
|_ __ | .' ____ \ |_ _ `. |_ _||_ _| |_ _| |_ _|
| |_ \_| | (___ \_| | | `. \ ______ \ \ / / | | | |
| _| _ _.____`. | | | | |______| \ \/ / | | | |
_| |__/ | | \____) | _| |_.' / _| |_ _| |_ _| |_
|________| \______.' |______.' |______| |_____| |_____|
Ready to generate the selected files? (yes|no) [yes]:yes
Files were generated successfully!
The following files will be generated:
[new] Model/CustomerModel.php
Generating code using template "/root/projects/esd-iot-warter-demo/vendor/bearlord/esd-framework/src/ESD/Yii/Gii/Generators/Model/default"...
generated Model/CustomerModel.php
done!
~~~
### 查看生成模型
~~~
<?php
namespace App\Model;
use ESD\Yii\Yii;
/**
* This is the model class for table "{{%customer}}".
*
* @property int $id
* @property int|null $wx_id 微信ID
* @property string|null $customer_name 客户姓名
* @property string|null $customer_username 客户姓名
* @property string|null $customer_password_hash 密码HASH
* @property int|null $created_at
* @property int|null $updated_at
*/
class CustomerModel extends \ESD\Yii\Db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return '{{%customer}}';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['wx_id', 'created_at', 'updated_at'], 'integer'],
[['customer_name'], 'string', 'max' => 180],
[['customer_username'], 'string', 'max' => 40],
[['customer_password_hash'], 'string', 'max' => 60],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'wx_id' => '微信ID',
'customer_name' => '客户姓名',
'customer_username' => '客户姓名',
'customer_password_hash' => '密码HASH',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
}
~~~
- 1 介绍
- 2 安装
- 2.1 环境
- 2.2 安装
- 3 配置
- 3.1 Server配置
- 3.2 端口配置
- 3.3 项目结构
- 3.4 内核优化
- 4 服务
- 4.1 HTTP服务
- 4.1.1 路由
- 4.1.1.1 静态路由
- 4.1.1.2 路由定义
- 4.1.1.3 路由方法
- 4.1.1.4 路由分组
- 4.1.1.5 资源路由
- 4.1.1.6 端口作用域
- 4.1.1.7 异常处理
- 4.1.1.8 跨域请求
- 4.1.1.9 路由缓存
- 4.1.2 控制器
- 4.1.2.1 控制器初始化
- 4.1.2.2 前置后置操作
- 4.1.2.3 跳转与重定向
- 4.1.2.4 异常处理
- 4.1.3 请求
- 4.1.3.1 请求对象
- 4.1.3.2 请求信息
- 4.1.3.3 REQUEST消息
- 4.1.3.4 RESPONSE消息
- 4.1.3.5 STREAM消息
- 4.1.3.6 URI信息
- 4.1.3.7 处理上传文件
- 4.1.3.8 验证器
- 4.2 Websocket服务
- 4.2.1 Websocket配置
- 4.2.2 Websocket路由
- 4.3 TCP服务
- 4.3.1 TCP配置
- 4.3.2 TCP路由
- 4.3.3 协程处理案例
- 5 插件
- 5.15 Yii-PDO插件
- 5.15.1 PDO 连接MySQL Mariadb
- 5.15.2 PDO连接PostgreSQL
- 5.15.3 PDO连接GreenPlum
- 5.15.4 PDO连接Oracle
- 5.15.5 PDO连接Cubrid
- 5.15.6 PDO连接SQL Server
- 6 概念
- 7. Yii- I18N国际化
- 8. 模型 Yii-Model
- 8.1 快速创建模型
- 8.2 快速创建多个模型
- 8.3 核心验证器 Core Validators
- 9. 配合数据库工作
- 9.1.数据库访问对象 Database Access Objects
- 9.2 查询构造器 Query Builder
- 9.3 活动记录 Active Record