手记

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

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


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

 ./yii migrate/create create_student_table

然后根据提示创建。

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

<?phpuse 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文件夹下。

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



作者:偏偏注定要落脚丶
链接:https://www.jianshu.com/p/75434054c62a

0人推荐
随时随地看视频
慕课网APP