PHP经验分享

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

2018-07-18  本文已影响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 生成模型代码。


生成模型代码

然后可能报错,如下:


报错代码
那么更改一下文件的权限即可。

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

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

上一篇 下一篇

猜你喜欢

热点阅读