具体5.7切换数据库即时

时间:2015-06-02 10:32:16

标签: database concrete5 concrete5-5.7

我想将表单结果写入另一个数据库。

在Concrete 5.6中,您可以动态切换到另一个数据库。我无法在混凝土5.7中找到它的工作原理。

5.6方式

$db = Loader::db( 'newserver', 'newuser', 'newpassword', 'newdatabase', true);

可以在Concrete 5.7中使用传统的Loader。我试图连接到数据库,但在错误中你可以看到它仍在尝试使用默认数据库。

$db = Database::get();
//tried this
$db = Loader::db('localhost', 'root', 'root', 'db_new', true);
//error
//db_original.db_new does not exist

1 个答案:

答案 0 :(得分:4)

我建议您在application / config / database.php中设置另一个连接。所以你的配置可能看起来像这样:

<?php

return array(
    'default-connection' => 'concrete',
    'connections' => array(
        'concrete' => array(
            'driver' => 'c5_pdo_mysql',
            'server' => 'localhost',
            'database' => 'c5',
            'username' => 'uuuuuuuuuuu',
            'password' => 'ppppppppppp',
            'charset' => 'utf8'
        ),

        'my_new_db' => array(
            'driver' => 'c5_pdo_mysql',
            'server' => 'localhost',
            'database' => 'db_new',
            'username' => 'uuuuuuuuuuu',
            'password' => 'ppppppppppp',
            'charset' => 'utf8'
        )
    )
);

然后,当您使用代码时,您可以通过执行以下操作随时访问该连接:

//get the default connection
$newDb = \Core::make('database')->connection();
//get the new connection
$newDb = \Core::make('database')->connection('my_new_db');

编辑:对于那些希望更改数据库驱动程序类型的人,您需要在配置中实际映射驱动程序实现,这可能看起来像这样:

<?php

return array(
    'default-connection' => 'concrete',

    'drivers' => array(
        'pdo_sqlsrv' => 'Doctrine\DBAL\Driver\SQLSrv\Driver'
    ),

    'connections' => array(
        'concrete' => array(
            'driver' => 'c5_pdo_mysql',
            'server' => 'localhost',
            'database' => 'c5',
            'username' => 'uuuuuuuuu',
            'password' => 'pppppppppp',
            'charset' => 'utf8',
        ),

        'my_new_db' => array(
            'driver' => 'pdo_sqlsrv',
            'server' => 'mydatabaseserver.mycompany.com',
            'database' => 'my_new_db',
            'username' => 'uuuuuuuuuuu',
            'password' => 'ppppppppppp',
        )
    ),
);