我在 codeigniter 中创建了一个迁移,以创建一个包含 4 列的 MySQL 表。现在我想更改其中一列的名称而不丢失该表中的数据。
首先,我启用了迁移并将版本设置为 1。
然后,我创建了一个文件 001_create_users.php
并添加了以下代码。
class Migration_Create_users extends CI_Migration {
public function up(){
$this->dbforge->add_field(
array(
'id' => array('type'=>'INT', 'constraint'=>11, 'unsigned'=>TRUE, 'auto_increment'=>TRUE),
'email' => array('type'=>'VARCHAR', 'constraint'=>100),
'password' => array('type'=>'VARCHAR', 'constraint'=>255),
'created' => array('type'=>"TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
)
);
$this->dbforge->add_key('id');
$this->dbforge->create_table('users');
}
public function down(){
$this->dbforge->drop_table('users');
}
}
现在我想将列名称“date
”更改为“created_at
”。那我该怎么办呢?我的意思是最好的方法是什么?
另外谁能解释一下如何进行回滚?因为我使用了以下 Controller 来运行迁移。
class Migration extends Admin_Controller{
public function __construct(){
parent::__construct();
}
public function index($version){
$this->load->library('migration');
if( !$this->migration->version($version)){
show_error($this->migration->error_string());
}
echo "I'm in Migration";
}
}
所以,如果我运行 http://localhost/migration/index/1它将运行 up() 函数。如何通过浏览器进行回滚?我可以创建另一个方法并调用它吗?
我仍然不明白迁移实际上是如何进行的,有人可以向我解释一下吗?
最佳答案
您可以尝试浏览http://localhost/migration/index/0
以返回到版本0。或者您可以创建单独的函数“rollback”以回滚到特定版本并创建索引仅使用 $this->migration->current()
关于php - Codeigniter 迁移以更改表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28291785/