【问题标题】:DB Forge, Code Igniter and dynamic database selectionDBForge、Codeigniter 和动态数据库选择
【发布时间】:2016-08-13 01:49:17
【问题描述】:

我想从一个数据库动态切换到另一个数据库,其名称已由用户动态指定。

以下是我的代码的一些重要片段:

//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-&gt;newDb-&gt;database=$database; 不是持久的。如果连接已关闭,然后重新打开,$this-&gt;newDb-&gt;database 将具有它最初在数据库配置文件中给出的值。但这并不能解释为什么我会得到这些结果,因为我没有关闭连接。

如果我将它添加到配置文件中:$db['newbase']['database'] = '';,那么我会收到 #1046 错误:未选择数据库。这证实了$this-&gt;newDb-&gt;database 未被dbforge 使用,它更喜欢使用硬编码值。

【问题讨论】:

    标签: php database codeigniter dbforge


    【解决方案1】:

    对于那些对答案感兴趣的人,这里就是。

    诀窍很简单:因为我不需要同时打开我的 2 个数据库,所以我只是使用 $this,就好像我在处理默认数据库一样。假设在配置文件中,我有一个名为“newbase”的第二组,其中有一个空的“数据库”字段。

    用户在设置过程中提供此名称。

    因此,一旦创建了数据库并关闭了连接:

    $this->load->database('newbase', True);     
    $this->db->database = '$database';
    

    使用$this-&gt;db-&gt;database 以了解正在处理的数据库。

    然后像上面一样声明 $linkfields,然后简单地调用 dbforge 类:

    $this->load->dbforge();
    $this->dbforge->add_field($linkfields);
    $this->dbforge->add_key('table_id', TRUE);  
    $this->dbforge->create_table('Link');
    

    【讨论】:

      猜你喜欢
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-02
      相关资源
      最近更新 更多