更改数据库连接的codeigniter迁移 [英] change database connection of codeigniter migration

查看:361
本文介绍了更改数据库连接的codeigniter迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改codeigniter迁移的数据库连接。

I would like to change database connection of codeigniter migration.

我的默认数据库是DB1,但我想更改与DB2的连接。

My default database is DB1, But I want to change connection to DB2.

我该怎么办?默认编码如下。

How can I do that? default coding as the following .

defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_Add_blog extends CI_Migration {

public function up()
{
    $this->dbforge->add_field(array(
        'blog_id' => array(
            'type' => 'INT',
            'constraint' => 5,
            'unsigned' => TRUE,
            'auto_increment' => TRUE
        ),
        'blog_title' => array(
            'type' => 'VARCHAR',
            'constraint' => '100',
        ),
        'blog_description' => array(
            'type' => 'TEXT',
            'null' => TRUE,
        ),
    ));

    $this->dbforge->create_table('blog');
}

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


推荐答案

dbforge库使用相同连接CI的其他数据库部分。由于只有一个连接,您必须更改该连接的数据库。如果您使用的是MYSQL,则可以使用两个 使用数据库调用

The dbforge library uses the same connection that CI's other database parts. Since there's only one connection, you would have to change the database of that connection. If you are on MYSQL, you can surround your code with two use database calls:

// switch to the second db
$this->db->query('use DB2');

// put DB2 stuff here

// switch back    
$this->db->query('use DB'); 

这篇关于更改数据库连接的codeigniter迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆