DBForge、Codeigniter 和动态数据库选择
DB Forge, Code Igniter and dynamic database selection
我想从一个数据库动态切换到另一个数据库,其名称已由用户动态指定。
以下是我的代码的一些重要片段:
//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');
这可行,但是...table 是在 'firstbase' 中创建的!尽管加载的数据库是第二个。我该如何解决这个问题?
编辑#1
显然,$this->newDb->database=$database;
并不持久。如果连接关闭,然后重新打开,$this->newDb->database
具有它最初在数据库配置文件中给出的值。但这并不能解释为什么我会得到这些结果,因为我没有关闭连接。
如果我将此添加到配置文件:$db['newbase']['database'] = '';
然后我会收到 #1046 错误:未选择数据库。这证实了 dbforge 不使用 $this->newDb->database
的事实,它更喜欢使用硬编码值。
对于那些对答案感兴趣的人,这里是。
技巧很简单:因为我不需要同时打开我的 2 个数据库,所以我只是使用 $this 就好像我在处理默认数据库一样。假设在配置文件中,我有一个名为 'newbase' 的第二组,其中有一个空的 'database' 字段。
用户在设置过程中给出了这个名字。
因此,一旦创建了数据库并关闭了连接:
$this->load->database('newbase', True);
$this->db->database = '$database';
使用 $this->db->database
以了解正在处理的数据库。
然后如上所述声明 $linkfields,并简单地调用 dbforge class :
$this->load->dbforge();
$this->dbforge->add_field($linkfields);
$this->dbforge->add_key('table_id', TRUE);
$this->dbforge->create_table('Link');
我想从一个数据库动态切换到另一个数据库,其名称已由用户动态指定。
以下是我的代码的一些重要片段:
//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');
这可行,但是...table 是在 'firstbase' 中创建的!尽管加载的数据库是第二个。我该如何解决这个问题?
编辑#1
显然,$this->newDb->database=$database;
并不持久。如果连接关闭,然后重新打开,$this->newDb->database
具有它最初在数据库配置文件中给出的值。但这并不能解释为什么我会得到这些结果,因为我没有关闭连接。
如果我将此添加到配置文件:$db['newbase']['database'] = '';
然后我会收到 #1046 错误:未选择数据库。这证实了 dbforge 不使用 $this->newDb->database
的事实,它更喜欢使用硬编码值。
对于那些对答案感兴趣的人,这里是。
技巧很简单:因为我不需要同时打开我的 2 个数据库,所以我只是使用 $this 就好像我在处理默认数据库一样。假设在配置文件中,我有一个名为 'newbase' 的第二组,其中有一个空的 'database' 字段。
用户在设置过程中给出了这个名字。
因此,一旦创建了数据库并关闭了连接:
$this->load->database('newbase', True);
$this->db->database = '$database';
使用 $this->db->database
以了解正在处理的数据库。
然后如上所述声明 $linkfields,并简单地调用 dbforge class :
$this->load->dbforge();
$this->dbforge->add_field($linkfields);
$this->dbforge->add_key('table_id', TRUE);
$this->dbforge->create_table('Link');