Yii 中的数据库迁移
为了在 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 文档但没有找到任何东西。因此,任何帮助将不胜感激。
In order to doing migration in Yii I used this lines
<?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()
{
}
*/
}
To get
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',
);
But now I want to make some changes like doing unique key
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`),
KEY `status` (`status`),
KEY `superuser` (`superuser`)
so how to make that? I searched Yii documentation but not found any thing. So any help will be highly appreciated..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将它们作为数组中的非关联值提供,如下所示:
这样它们将被添加到 create 语句的末尾而不进行任何更改。
Just supply them as non-associative values in the array like this:
This way they will be added to the end of the create statement without any change.
请参阅http://www.yiiframework.com/doc/api/1.1/CDbMigration#createIndex-detail 了解更多详情。
See http://www.yiiframework.com/doc/api/1.1/CDbMigration#createIndex-detail for more details.