In EE2 we were able to connect to a secondary external database from within our plugins. In EE3 this doesn’t appear to work anymore. In our config.php file we have:
$config['database'] = array (
'expressionengine' =>
array (
'hostname' => 'localhost',
'username' => 'ee_user',
'password' => 'ee_pass',
'database' => 'ee_database',
'db_debug' => false,
),
'second_database' =>
[
'hostname' => 'localhost',
'username' => 'second_user',
'password' => 'second_pass',
'database' => 'second_db',
'db_debug' => false,
],
);
In the plugin we have:
$db2 = ee()->load->database('second_database',true);
We get the warnings:
Illegal string offset 'hostname'
ee/legacy/database/DB.php, line 34
It appears that even though we setup everything to use a different database, EE is still connecting to the primary database. How do we fix this so we can access our second DB?
not sure why the config you posted ever worked. Try this:
$config['database'] = array (
'expressionengine' => array (
'hostname' => 'localhost',
'username' => 'ee_user',
'password' => 'ee_pass',
'database' => 'ee_database',
'db_debug' => false,
),
'second_database' => array (
'hostname' => 'localhost',
'username' => 'second_user',
'password' => 'second_pass',
'database' => 'second_db',
'db_debug' => false,
),
);
What works in EE3 for us is to have this in the database config file:
$db['expressionengine']['hostname'] = "localhost";
$db['expressionengine']['username'] = "username";
$db['expressionengine']['password'] = "password";
$db['expressionengine']['database'] = "database";
$db['expressionengine']['dbdriver'] = "mysql";
$db['expressionengine']['dbprefix'] = "exp_";
$db['expressionengine']['pconnect'] = FALSE;
$db['expressionengine']['swap_pre'] = "exp_";
$db['expressionengine']['db_debug'] = FALSE;
$db['expressionengine']['cache_on'] = FALSE;
$db['expressionengine']['autoinit'] = FALSE;
$db['expressionengine']['char_set'] = "utf8";
$db['expressionengine']['dbcollat'] = "utf8_general_ci";
$db['expressionengine']['cachedir'] = "/path/to/eecms/cache/db_cache/";
$db['second_database']['hostname'] = "localhost";
$db['second_database']['username'] = "username2";
$db['second_database']['password'] = "password2";
$db['second_database']['database'] = "database2";
$db['second_database']['dbdriver'] = "mysql";
$db['second_database']['dbprefix'] = "";
$db['second_database']['pconnect'] = FALSE;
$db['second_database']['swap_pre'] = "";
$db['second_database']['db_debug'] = true;
$db['second_database']['cache_on'] = FALSE;
$db['second_database']['autoinit'] = FALSE;
$db['second_database']['char_set'] = "utf8";
$db['second_database']['dbcollat'] = "utf8_general_ci";
$db['second_database']['cachedir'] = "/path/to/eecms/cache/db_cache/";
And the following in our plugin:
$db_config = new Database\DBConfig(ee('Config')->getFile());
$db_config->getGroupConfig('second_database');
$connection = new Database\Database($db_config);
$db2 = $connection->newQuery();
$query = $db2->query('SELECT something FROM db')
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.