CodeIgniter迁移:更多数据库

时间:2016-07-25 10:18:45

标签: php database codeigniter database-migration

我尝试使用更多数据库进行迁移:默认数据库和另一个数据库。 这是database.php文件:

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

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
'dsn'   => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => '',
'database' => 'buy_prova_test',
'dbdriver' => 'mysqli',
'dbprefix' => 'ext_',
'pconnect' => FALSE,
//'db_debug' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['oauth_test'] = array(
'dsn'   => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => '',
'database' => 'oauth_test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
//'db_debug' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

?>

相反,这是第一次正常运行的迁移:

<?php
class Migration_Create_external_site_manager extends CI_Migration
{
public function up()
{
    $this->dbforge->add_field("
     ID bigint(20) NOT NULL AUTO_INCREMENT,
     sito varchar(255) NOT NULL,
     url_override text NOT NULL,
     title varchar(255) NOT NULL,
     title_facebook text NOT NULL,
     description text NOT NULL,
     description_facebook text NOT NULL,
     description_twitter text NOT NULL,
     img_facebook text NOT NULL,
     img_twitter text NOT NULL,
     PRIMARY KEY (ID)"
    );
    $this->dbforge->create_table('external_site_manager');
}

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

这是我在oauth_test数据库上进行的第二次迁移,它没有返回错误,但我没有看到数据库中的任何表:

<?php
class Migration_Create_oauth_test extends CI_Migration
{


public function up()
{
    $oauth=$this->load->database('oauth_test',true);
    $this->oauth_forge=$this->load->dbforge($oauth,TRUE));
    $this->oauth_forge->add_field("
        access_token varchar(40) NOT NULL,
        client_id varchar(80) NOT NULL,
        user_id varchar(255) DEFAULT NULL,
        expires timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
        scope varchar(2000) DEFAULT NULL,
        PRIMARY KEY (access_token)");
    $this->oauth_forge->create_table("oauth_access_tokens");
    $this->oauth_forge->add_field("
        client_id varchar(80) NOT NULL,
         client_secret varchar(80) NOT NULL,
         redirect_uri varchar(2000) NOT NULL,
         grant_types varchar(80) DEFAULT NULL,
         scope varchar(100) DEFAULT NULL,
         user_id varchar(80) DEFAULT NULL,
         PRIMARY KEY (client_id)
        ");
    $this->oauth_forge->create_table("oauth_clients");
}

public function down()
{
    $oauth=$this->load->database('oauth_test',true);
    $this->oauth_forge=$this->load->dbforge($oauth,TRUE));
    $this->oauth_forge->drop_table("oauth_clients");
    $this->oauth_forge->drop_table("oauth_access_tokens");
}
}

?>

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我发现自己是解决方案,如果每个人都需要帮助,我会发布它:

$this->db->close(); /*close connection to default*/
$this->db = $this->load->database('oauth_test',true);/*switch to oauth_test*/
$this->myforge=$this->load->dbforge($this->db, TRUE);