php - Codeigniter 迁移以更改表

标签 php mysql codeigniter migration database-migration

我在 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/

相关文章:

PHP MySQLi : String being converted to int?

mysql 测验排行榜按分数、花费时间过滤

mysql - 第 2 行存在语法错误 (1064)

Codeigniter $this->db->affected_rows() 总是返回 1

php - (codeigniter/Neo4j) PHP : namespace and autoloader

php - Silverstripe 4 - 访问核心方法

遇到PHP7非数值

php - WORDPRESS:我刚刚在 wp_user 行上使用 phpMyadmin 创建了一个额外的列,现在我需要每个用户在登录时更新该列

php - 规范 : How to save HTML form data into MySQL database

php - 从 codeigniter 迁移到 Laravel