我想动态地从数据库切换到另一个,其名称已由用户动态给出。DB伪造,代码点火器和动态数据库选择
这里是我的代码一些有意义的片段:
//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'];
正如你所看到的,“newbase”是默认配置的简单复制现在
//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;
,我想dbforge是用于这个新的数据库。
$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');
这种方法可行,但...表格是在'firstbase'中创建的!尽管加载的数据库是第二个。我该如何解决这个问题?
编辑#1
显然,$this->newDb->database=$database;
是不持久的。如果连接关闭,然后重新打开,$this->newDb->database
具有最初在数据库配置文件中给出的值。但是这并不能解释为什么我会得到这些结果,因为我没有关闭这个关系。
如果我将此添加到配置文件:$db['newbase']['database'] = '';
然后我得到#1046错误:未选择数据库。这证实了dbforge没有使用$this->newDb->database
这一事实,它更喜欢使用硬编码值。