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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀