YII2(一)用YII2创建、迁移数据表 migrations

来源:互联网 发布:联想win7还原软件 编辑:程序博客网 时间:2024/05/22 14:14

    YII2属于PHP开发框架的一种,主要应用于应用中大型WEB开发。

    下面为YII2的配置数据库连接。

首先为了安全考虑,设置不保存本地的配置信息,在公共的common/config/.gitignore中设置。

   

下面,讲一下YII2数据库的创建命令。



创建成功后,会有m+时间戳+命名.php文件生成


其中,在类中可以写up()方法,还有down()方法;

up()则是创建一张表,而down()则是删除这张表;


如下面代码:


public function safeUp(){    $tableOptions = null;    if ($this->db->driverName === 'mysql') {        // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';    }    $this->createTable(USER_TABLE, [        'id' => Schema::TYPE_PK,        'username' => Schema::TYPE_STRING . ' NOT NULL',        'auth_key' => Schema::TYPE_STRING . '(32) NOT NULL',        'password_hash' => Schema::TYPE_STRING . ' NOT NULL',        'password_reset_token' => Schema::TYPE_STRING,        'email' => Schema::TYPE_STRING . ' NOT NULL',        'status' => Schema::TYPE_SMALLINT . ' NOT NULL DEFAULT 10',        'created_at' => Schema::TYPE_INTEGER . ' NOT NULL',        'updated_at' => Schema::TYPE_INTEGER . ' NOT NULL',    ], $tableOptions);    /* 创建索引 1,索引名称 2,表名 3,数组,使用哪些字段创建索引值 4,默认为false,是否是唯一性的*/    $this->createIndex('user',USER_TABLE,['username'],true);    $this->createIndex('email',USER_TABLE,['email'],true);}

之后,运行该脚本



此时会生成两张表


其中,migration为yii2框架自带的表,用于生成数据库迁移用。

user则为刚刚创建的表



接下来,在命令行中,创建初始数据,这样更便捷,易于以后的维护。


创建初始数据的操作,可以使用gii图形工具来进行创建。


http://localhost:84/index.php?r=gii

进入 Model Generator中,

自动生成后的models代码

<?php

namespace app\models;

use 
Yii;

/**
 * This is the model class for table "{{%User}}".
 *
 * @property integer $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property integer $status
 * @property integer $created_at
 * @property integer $updated_at
 */
class User extends \yii\db\ActiveRecord
{
    
/**
     * @inheritdoc
     */
    
public static function tableName()
    {
        return 
'{{%User}}';
    }

    
/**
     * @inheritdoc
     */
    
public function rules()
    {
        return [
            [[
'username''auth_key''password_hash''email''created_at''updated_at'], 'required'],
            [[
'status''created_at''updated_at'], 'integer'],
            [[
'username''password_hash''password_reset_token''email'], 'string''max' => 255],
            [[
'auth_key'], 'string''max' => 32],
            [[
'username'], 'unique'],
            [[
'email'], 'unique']
        ];
    }

    
/**
     * @inheritdoc
     */
    
public function attributeLabels()
    {
        return [
            
'id' => Yii::t('app''ID'),
            
'username' => Yii::t('app''Username'),
            
'auth_key' => Yii::t('app''Auth Key'),
            
'password_hash' => Yii::t('app''Password Hash'),
            
'password_reset_token' => Yii::t('app''Password Reset Token'),
            
'email' => Yii::t('app''Email'),
            
'status' => Yii::t('app''Status'),
            
'created_at' => Yii::t('app''Created At'),
            
'updated_at' => Yii::t('app''Updated At'),
        ];
    }
}






0 1
原创粉丝点击