企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 快速创建模型 快速创建模型 整合自 `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',       ];   } } ~~~