データベースから動的に名前を変更した別のデータベースに切り替えることができます。ここでDBフォージ、コードイグナイタ、動的データベース選択
は、私のコードの一部significative断片である:あなたが見ることができるように
//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;
の単純なコピーですが、私はDBフォージになりたいですこの新しいデータベースで使用されます。
$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」で作成されます。そして、読み込まれたデータベースは2番目のものです。これをどうすれば解決できますか?
EDIT#1
はどうやら、$this->newDb->database=$database;
は永続的ではありません。コネクションがクローズされていて再オープンされた場合、$this->newDb->database
の値は最初にデータベース設定ファイルに与えられます。しかし、これは私がコネクションを閉じないので、なぜ私はそれらの結果を得るのか説明しません。
これを設定ファイルに追加すると、$db['newbase']['database'] = '';
と表示されますが、#1046エラーが表示されます。データベースが選択されていません。これは、$this->newDb->database
がdbforgeで使用されていないことを確認します。dbforgeはハードコードされた値を代わりに使用します。