yii 2 gii工具 模型生成器的使用方法
来源:互联网 发布:北京物资学院网络课 编辑:程序博客网 时间:2024/05/16 19:47
众所周知 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
是否生产数据表各个标签对应的注释下面我们来生产一个表单做实验
如图生产表单
生产的模型类代码如下
<?phpnamespace 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 那效果将是这样
生成的代码如下
<?phpnamespace 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' => '头像', ]; }}
两者的不同 我相信你肯定已经看出来了 生产代码如此方便 仔细学习 相信一定能节约你不少的时间
阅读全文
0 0
- yii 2 gii工具 模型生成器的使用方法
- yii gii的使用方法
- yii gii的使用方法
- Yii gii的使用方法
- YII框架yiic、gii工具使用方法
- yii gii的使用
- yii gii的使用
- Yii 1.1代码生成器gii(booster gii)模板修改
- 20. Yii 2.0 gii 代码生成器
- 20. Yii 2.0 gii 代码生成器
- Yii框架中的gii工具
- yii下gii代码生成工具的使用
- Yii学习(4)----Gii的使用
- 配置使用yii 自动化代码生成工具gii(脚手架)
- YII 之gii(自动代码生成工具)使用
- yii学习笔记—gii 自动代码生成工具
- yii 2.0 使用gii工具生成代码(四)
- Yii framework2 gii配置
- RNN&LSTM&GRU
- js实现有关时间的倒计时
- 【LeetCode】169. Majority Element解题报告
- sql注入的解决方法(注 代码)
- C语言 学生信息增删改查
- yii 2 gii工具 模型生成器的使用方法
- 重载“=”运算符
- 微信小程序从使用到分析快速解析
- Ubuntu14.04 安装openjdk7和openjdk8与切换
- python初学
- tomcat8.5.15 apr模式运行 配置
- 泛型反射
- Linux下安装Redis以及配置远端访问
- Array Nesting (第十四周 数组)