美文网首页PHP经验分享
Yii2.0开发——使用Gii生成代码的简单实践

Yii2.0开发——使用Gii生成代码的简单实践

作者: 偏偏注定要落脚丶 | 来源:发表于2018-07-18 14:24 被阅读33次

下面以一个简单的学生信息为例介绍Gii的简单使用方法。


  • 首先利用数据书迁移脚本创建数据库(默认数据库连接已经配置成功)。
    输入命令:
 ./yii migrate/create create_student_table

然后根据提示创建。

*使用数据库迁移文件创建数据库。完善该文件如下:

<?php

use yii\db\Migration;

/**
 * Handles the creation of table `student`.
 */
class m180718_031403_create_student_table extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->createTable('student', [
            'id' => $this->primaryKey(),
            'number' => $this->integer()->notNull()->unique()->comment("学号"),
            'name' => $this->string(20)->notNull(),
            'gender' => $this->integer()->notNull()->comment("0:未知  1:男  2:女"),
            'class' => $this->integer()->notNull()->comment("班级")
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('student');
    }
}

然后执行

./yii migrate

命令,根据提示完成。

然后点击 Model Generator 生成模型代码。


生成模型代码

然后可能报错,如下:


报错代码
那么更改一下文件的权限即可。
  • 创建模型的控制器等代码


    生成CRUD代码

上图StudentController的路径有误,应写到controllers文件夹下。

这样就完成了简单的查询的代码生成。

相关文章

网友评论

  • 诺晓仁:楼主写的太好了,找了很久这个,很适合小白看

本文标题:Yii2.0开发——使用Gii生成代码的简单实践

本文链接:https://www.haomeiwen.com/subject/hwawpftx.html