yii 2 gii工具 模型生成器的使用方法

众所周知 Yii 2 是一个非常高效的 PHP 框架 它之所以高效 有部分原因在于 它的模型生成器非常方便 可以帮助我们生成代码 避免了 我们的重复劳作

下面我就将详细的介绍一下 Yii 2 框架中 gii模型生产器 的具体使用方法 来降低大家的代码重复量 从而体会到编程过程中思考的乐趣

首先我们需要打开gii工具 打开的方法 非常容易 打开网页即可 输入 http://localhost/yii2/web/gii 便可打开gii工具

效果显示图如下

这里写图片描述

ps(我使用的是 PATH_INFO 格式输入网址 并隐藏了index.php文件 Yii2初始登录gii的方式应该为 http://localhost/yii2/web/index.php/?r=gii

我们点进 Model Generator

会看到下面这样的情况

这里写图片描述

下面我来详细介绍一下 gii模型生成器 各个主要选项的 作用是什么

  • Table Name
    数据表的名称

  • Model Class
    我们要生成模型类的名称

  • Use Table Prefix
    是否使用表前缀

  • Generate Labels from DB Comments
    是否生产数据表各个标签对应的注释

    下面我们来生产一个表单做实验

如图生产表单

这里写图片描述

生产的模型类代码如下

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "study_student".
 *
 * @property string $id
 * @property string $s_no
 * @property string $s_user
 * @property string $s_nation
 * @property string $s_sex
 * @property string $s_major
 * @property string $s_class
 * @property string $head
 */
class Student extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'study_student';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['s_no'], 'string', 'max' => 12],
            [['s_user'], 'string', 'max' => 50],
            [['s_nation', 's_sex'], 'string', 'max' => 5],
            [['s_major'], 'string', 'max' => 20],
            [['s_class'], 'string', 'max' => 10],
            [['head'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            's_no' => 'S No',
            's_user' => 'S User',
            's_nation' => 'S Nation',
            's_sex' => 'S Sex',
            's_major' => 'S Major',
            's_class' => 'S Class',
            'head' => 'Head',
        ];
    }
}

如果我使用了表前缀并勾选了 Generate Labels from DB Comments 那效果将是这样

这里写图片描述

生成的代码如下

<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "{{%student}}".
 *
 * @property string $id
 * @property string $s_no
 * @property string $s_user
 * @property string $s_nation
 * @property string $s_sex
 * @property string $s_major
 * @property string $s_class
 * @property string $head
 */
class Student extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%student}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['s_no'], 'string', 'max' => 12],
            [['s_user'], 'string', 'max' => 50],
            [['s_nation', 's_sex'], 'string', 'max' => 5],
            [['s_major'], 'string', 'max' => 20],
            [['s_class'], 'string', 'max' => 10],
            [['head'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '学生的id',
            's_no' => '学生的学号',
            's_user' => '学生的名字',
            's_nation' => '民族',
            's_sex' => '性别',
            's_major' => '专业',
            's_class' => '班级',
            'head' => '头像',
        ];
    }
}

两者的不同 我相信你肯定已经看出来了 生产代码如此方便 仔细学习 相信一定能节约你不少的时间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值