I would like to set up database credentials dynamically in the application lifecycle. Since I could not do it from the class function where am calling my editor autoloader I have opted to edit the .config file of the datatable editor to this
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" => "",
);
However going by my logs it seems I cannot access the session variable
$_SESSION['MY_DATA']['tenant_uuid']
.
What am I missing?
I finally found a workaround. All I needed to do is to include the DataTables\Database namespace in the function file and proceed to creating a new database instance. Changing globals in a function is quite tricky.
No need to get messy with editing the config.php file.
Final solution:
use
DataTables\Database,
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
In the function
$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 );