0

我想在应用程序生命周期中动态设置数据库凭据。由于我无法从调用我的编辑器自动加载器的类函数中执行此操作,因此我选择将数据表编辑器的 .config 文件编辑为此

require_once('../functions/functions.php');

//get db credentials
$db_credentials = get_db_credentials($_SESSION['MY_DATA']['tenant_uuid']);

$sql_details = array(
            "type" => "Mysql",
            "user" => $db_credentials['datastore_db_username'],
            "pass" => $db_credentials['datastore_db_password'],
            "db"   => $db_credentials['datastore_db_name'],
            "host" => $db_credentials['datastore_db_host'],
            "dsn"  => "", 
            "port" => "",
        );

但是根据我的日志,我似乎无法访问会话变量

$_SESSION['MY_DATA']['tenant_uuid'].

我错过了什么?

4

1 回答 1

0

我终于找到了解决方法。我需要做的就是在函数文件中包含DataTables\Database命名空间,然后继续创建一个新的数据库实例。在函数中更改全局变量非常棘手。

无需为编辑 config.php 文件而弄得一团糟。

最终解决方案:

   use
       DataTables\Database,
       DataTables\Editor,
       DataTables\Editor\Field,
       DataTables\Editor\Format,
       DataTables\Editor\Mjoin,
       DataTables\Editor\Options,
       DataTables\Editor\Upload,
       DataTables\Editor\Validate;

在函数中

$db_credentials = get_db_credentials($_SESSION['MY_DATA']['tenant_uuid']);

$sql_details = array(
    "type" => "Mysql",
    "user" => $db_credentials['datastore_db_username'],
    "pass" => $db_credentials['datastore_db_password'],
    "db"   => $db_credentials['datastore_db_name'],
    "host" => $db_credentials['datastore_db_host'],
    "dsn"  => "", 
    "port" => "",
);

$db_details = new Database( $sql_details );
于 2017-06-30T12:15:12.193 回答