doushi1974 2016-08-12 15:09
浏览 88
已采纳

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 2016-08-12 17:49
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真