举例说明 Yii2 中 addForeignKey 的使用 [ 新手入门 ]
在console/migrations下,文件 m130524_201442_init.php // user表
<?php
use yii\db\Migration;
class m130524_201442_init extends Migration
{
public function up()
{
$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}}', [
'id' => $this->primaryKey(),
'addid' => $this->integer()->notNull(),
'username' => $this->string()->notNull()->unique(),
'auth_key' => $this->string(32)->notNull(),
'password_hash' => $this->string()->notNull(),
'password_reset_token' => $this->string()->unique(),
'email' => $this->string()->notNull()->unique(),
'status' => $this->smallInteger()->notNull()->defaultValue(10),
'created_at' => $this->integer()->notNull(),
'updated_at' => $this->integer()->notNull(),
], $tableOptions);
$this->createIndex('addid', self::TABLE_NAME , ['addid'] , false);
}
public function down()
{
$this->dropTable('{{%user}}');
}
}
?>
m160912_171446_user_add.php // useradd表
<?php
use yii\db\Migration;
class m160912_171446_user_add extends Migration
{
const TABLE_NAME = '{{%user_add}}';
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName==='mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable(self::TABLE_NAME,[
'addid' => $this->primaryKey()->comment('用户信息ID'),
'gender' => $this->smallInteger()->notNull()->comment('用户性别'),
'country' => $this->string()->notNull()->comment('国家'),
'location' => $this->string()->notNull()->comment('城市'),
'address' => $this->string()->notNull()->comment('地址'),
'userimg' => $this->string()->notNull()->comment('用户头像'),
'email_2' => $this->string()->notNull()->comment('公开邮箱'),
'nickname' => $this->string()->notNull()->comment('昵称'),
'website' => $this->string()->notNull()->comment('个人主页'),
'qq' => $this->string()->notNull()->comment('QQ'),
'school' => $this->string()->notNull()->comment('学校'),
'company' => $this->string()->notNull()->comment('公司'),
'birthday' => $this->string()->notNull()->comment('生日'),
'signature' => $this->string()->notNull()->comment('个性签名'),
],$tableOptions);
$this->addForeignKey('xxx_user_add_addid' , self::TABLE_NAME , '[[addid]]' ,
'{{%user}}' , '[[addid]]' , 'CASCADE' , 'RESTRICT');
}
public function safeDown()
{
$this->dropTable(self::TABLE_NAME);
}
/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}
public function safeDown()
{
}
*/
}
?>
$this->addForeignKey() 如何使用?
$this->addForeignKey('xxx_user_add_addid' , self::TABLE_NAME , '[[addid]]' , '{{%user}}' , '[[addid]]' , 'CASCADE' , 'RESTRICT');
这行代码有什么意思? addForeignKey() 函数位置:vendor/yiisoft/yii2/db/Migration.php
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null)
明确两个概念: 外表(%user)和本表(%user_add),外表即约束表,本表是被外表约束的。 如何约束呢?使用 addForeignKey函数,给本表添加外键
本表的addid与外表的addid设置互动关系
总共有这么几种关系:RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL
RESTRICT:和 NO ACTION 是一样的, 如果本表中有匹配的记录,则不允许进行update/delete操作
CASCADE:外表删除的时候,本表删除,或者外表更新的时候本表更新
SET NULL:在外表上update/delete记录时,将本表上匹配记录的列设为null,要注意本表的外键列不能为not null
SET DEFAULT: 外表有变更时,本表将外键列设置成一个默认的值 但Innodb不能识别
现在来看看yii2 的这句代码:$this->addForeignKey('xxx_user_add_addid' , self::TABLE_NAME , '[[addid]]' , '{{%user}}' , '[[addid]]' , 'CASCADE' , 'RESTRICT');
设置本表的外键名为:xxx_user_add_addid
本表的字段 addid 与 外表addid 为互动关系
当删除外表'{{%user}}',某个addid的时候,本表'{{%user_add}}',对应的addid列也会删除
当更新外表'{{%user}}',某个addid的时候,本表'{{%user_add}}',对应的addid列不做改动
注:记得给外表的字段addid加索引,否则无法添加为外键。
共 0 条回复
qiuxis
最后登录:2017-10-25
在线时长:22小时6分
- 粉丝21
- 金钱215
- 威望40
- 积分835