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'),
];
}
}
- YII2(一)用YII2创建、迁移数据表 migrations
- YII2(一)用YII2创建、迁移数据表 migrations
- yii2基础(一)
- Yii2- 数据表前缀设置
- yii2 Ecommerce - (数据迁移) 索引注释
- Yii2之数据库迁移(Migrate)
- yii2 migrate 数据库迁移
- Yii2创建控制器(createController)
- Yii2创建表单(ActiveForm)
- yii2学习笔记(一)
- YII2
- YII2
- Yii2
- yii2
- Yii2
- Yii2.0学习笔记(一)之Yii2.0起步
- yii2简述一对一数据表关联
- yii2文件(图片)上传,url保存到数据表
- Length of Last Word
- jQuery 全选 反选 超简单示例
- 安卓微信朋友圈界面
- 在linux上使用yum安装jdk
- solr 5.0.0 新手快速入门
- YII2(一)用YII2创建、迁移数据表 migrations
- NetworkInterface的使用
- Mysql 存储引擎中InnoDB与MyISAM区别(网络整理)
- Android可滚动视图隐藏Toolbar实现
- Python XML-PRC server
- 随笔(2015.3)
- 番茄花园Ghost Win10系统X64位10041装机版2015年4月
- 背包问题求解 - java
- stm32学习笔记---串口(原子开发板)