如何更改迁移数据库连接

时间:2015-12-09 06:58:28

标签: codeigniter-3

我在config/database.php中有多个连接设置,我可以并且必须连接到同一个案例中的多个数据库,这是我尝试但不工作的方式。

<?php

class Migration_Create_loggers_table extends CI_Migration
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database('master');
    }

    public function up()
    {
        $this->dbforge->add_field([
            'id' => [
                'type' => 'int',
                'unsigned' => true,
                'auto_increment' => true
            ],
            'name' => [
                'type' => 'varchar',
                'constraint' => 50
            ]
        ]);

        $this->dbforge->add_key('id', true);
        $this->dbforge->create_table('members');
    }

    public function down()
    {
        $this->dbforge->drop_table('members');
    }
}

1 个答案:

答案 0 :(得分:0)

我没有尝试,但我认为你错过了parent::__construct();方法的__construct

因此,您的__construct方法应该是这样的

class Migration_Create_loggers_table extends CI_Migration
{
    public function __construct()
    {
        $this->load->database('master');
        parent::__construct();//add this line
    }

    public function up()
    {
        $this->dbforge->add_field([
            'id' => [
                'type' => 'int',
                'unsigned' => true,
                'auto_increment' => true
            ],
            'name' => [
                'type' => 'varchar',
                'constraint' => 50
            ]
        ]);

        $this->dbforge->add_key('id', true);
        $this->dbforge->create_table('members');
    }

    public function down()
    {
        $this->dbforge->drop_table('members');
    }
}