gpt4 book ai didi

php - Yii 中的数据库迁移

转载 作者:可可西里 更新时间:2023-11-01 06:53:17 25 4
gpt4 key购买 nike

为了做migration in Yii我用了这条线

<?php

class m110714_122129_users extends CDbMigration
{
public function up()
{
$this-> createTable('{{users}}',array(
'id' => 'pk',
'username' => 'VARCHAR (80) NOT NULL',
'password' => 'VARCHAR (80) NOT NULL',
'email' => 'VARCHAR (128) NOT NULL',
'activekey' => 'VARCHAR (128) NOT NULL DEFAULT \'\'',
'createtime' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
'lastvisit' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
'superuser' => 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
'status'=> 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
));

}

public function down()
{
echo "m110714_122129_users does not support migration down.\n";
return false;
}

/*
// Use safeUp/safeDown to do migration with transaction
public function safeUp()
{
}

public function safeDown()
{
}
*/
}

得到

 CREATE TABLE IF NOT EXISTS `nt_users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(128) NOT NULL,
`email` varchar(128) NOT NULL,
`activkey` varchar(128) NOT NULL DEFAULT '',
`createtime` int(10) NOT NULL DEFAULT '0',
`lastvisit` int(10) NOT NULL DEFAULT '0',
`superuser` int(1) NOT NULL DEFAULT '0',
`status` int(1) NOT NULL DEFAULT '0',
);

但现在我想做一些改变,比如做唯一键

UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`),
KEY `status` (`status`),
KEY `superuser` (`superuser`)

那么怎么做呢?我搜索了 Yii 文档但没有找到任何东西。因此,我们将不胜感激任何帮助。

最佳答案

只需像这样在数组中将它们作为非关联值提供:

$this-> createTable('{{users}}',array(
'id' => 'pk',
'username' => 'VARCHAR (80) NOT NULL',
'password' => 'VARCHAR (80) NOT NULL',
'email' => 'VARCHAR (128) NOT NULL',
'activekey' => 'VARCHAR (128) NOT NULL DEFAULT \'\'',
'createtime' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
'lastvisit' => 'INTEGER (10) NOT NULL DEFAULT \'0\' ',
'superuser' => 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
'status'=> 'INTEGER (1) NOT NULL DEFAULT \'0\' ',
'UNIQUE KEY `username` (`username`)',
'UNIQUE KEY `email` (`email`)',
'KEY `status` (`status`)',
'KEY `superuser` (`superuser`)',
));

这样它们将被添加到 create 语句的末尾而不做任何更改。

关于php - Yii 中的数据库迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6693134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com