php-activerecord
php-activerecord copied to clipboard
Read connection details from array
It would be nice to set connection details from an array. Now it is already so that the connection string is parsed to an object. So converting from an array shouldn't be that much of a problem.
The reason I would like this is because I read the connection details from a .yml
file, which returns me an array in the form of:
array(
'development' => array(
'adapter' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database_name',
'username' => 'webuser',
'password' => 'webpass'
),
'production' => array(
'adapter' => 'mysql',
'host' => '127.0.0.1',
'database' => 'database_name',
'username' => 'webuser',
'password' => 'webpass'
)
);
Which I now convert to connection strings using the following array_map
:
$connections = array_map(function( $value ){
return "${value['adapter']}://${value['username']}:${value['password']}@${value['host']}/${value['database']}";
}, $config);
I will submit a PR for this "soon".
This makes sense to me, and is how configurations work in most PHP projects/libraries.
Agreed. Where's that PR, @koenpunt ...? :D
@al-the-x here is my PR: #494 (not really "soon")
:+1: Keep on truckin'...
When trying to use a password with special characters (such as @
or ?
), the connection fails. I believe this is because the connection string is a URI and it becomes malformed. Example (password is p@ssw?rd
):
mysql://root:p@ssw?rd@localhost:3306/myschema?charset=utf8mb4
Would this array method fix this? Or is there another solution in these cases?