doushi1974
doushi1974
2016-08-12 15:09

DBForge,Codeigniter和动态数据库选择

已采纳

I'd like to dynamically switch from a database to another one, whose name has been dynamically given by the user.

Here are some significative fragments of my code :

//database.php

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

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'firstbase',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => 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['newbase'] = $db['default'];

As you can see, 'newbase' is a simple copy of the default config

//Install_model Model
    echo "<BR>Active db :".$this->db->database;
    $this->createDB($database);
    $this->db->close();

    //now $database is created

    //load the 'newbase' group and assign it to $this->newDb
    $this->newDb = $this->load->database('newbase', TRUE);

    //this will output 'firstbase'
    echo "<BR>so far, the active db is :".$this->newDb->database;
    $this->newDb->database=$database;
    //now this will output the $database string
    echo "<BR>and now the active db is :".$this->newDb->database;

Now, I want dbforge to be used on this new database.

$linkfields = array(
            'table_id' => array(
                    'type' => 'VARCHAR',
                    'constraint' => '15'
            ),
            'something' => array(
                    'type' => 'VARCHAR',
                    'constraint' => '15',
                    'unique' => TRUE,
            ),
        );

        //according to the manual, we "give" our new DB to dbforge
        $this->myforge = $this->load->dbforge($this->newDb, TRUE);
        $this->myforge->add_field($linkfields);
        $this->myforge->add_key('table_id', TRUE);
        $this->myforge->create_table('Link');

This works but...the table is created in 'firstbase' ! And though the database that is loaded is the second one. How can I solve this ?

EDIT #1

Apparently, $this->newDb->database=$database; is not persistent. If the connexion is closed, then re-opened, $this->newDb->database has the value it is initially given in the database config file. But this doesn't explain why I get those results, as I don't close the connexion.

If I add this to the config file : $db['newbase']['database'] = ''; then I get a #1046 error : No database selected. This confirms the fact that $this->newDb->database is not used by dbforge, which prefers to use the hard-coded value instead.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • donglan8256 donglan8256 5年前

    For those who are interested in the answer, here it is.

    The trick is quite simple : as I didn't neet my 2 dbs to be open at the same time, I just used the $this as if I was handling the default db. Let's assume that in the config file, I have a second group named 'newbase' with an empty 'database' field.

    The user gives this name during the setup process.

    So, once the db is created and the connexion closed :

    $this->load->database('newbase', True);     
    $this->db->database = '$database';
    

    Use $this->db->database in order to know what db is being handled.

    then declare the $linkfields as above, and simply call the dbforge class :

    $this->load->dbforge();
    $this->dbforge->add_field($linkfields);
    $this->dbforge->add_key('table_id', TRUE);  
    $this->dbforge->create_table('Link');
    
    点赞 评论 复制链接分享

相关推荐