dbforge库使用与CI的其他数据库部分相同的连接。由于只有一个连接,您必须更改该连接的数据库。如果您在MYSQL上,您可以用两个
我想更改代码点火器迁移的数据库连接。
我的默认数据库是DB1,但我想更改与DB2的连接。
我该怎么做?默认编码如下。
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');
}
use database
调用包围您的代码:
// switch to the second db
$this->db->query('use DB2');
// put DB2 stuff here
// switch back
$this->db->query('use DB');
您可以使用
$this->load->database();
用于更改数据库连接。在config/database.php中,您可以定义多个数据库连接。例如,要更改为系统数据库,请使用以下命令:
$this->load->database('system');
我使用它进行迁移,并将命令放在迁移控制器的构造函数中。
class Migrate extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database('system'); //Use the system-Connection for DB-Manipulation
$this->load->library('migration');
}
/**
* Migrate to latest or current version
* @return void
*/
public function index()
{
...