php-activerecord icon indicating copy to clipboard operation
php-activerecord copied to clipboard

Read connection details from array

Open koenpunt opened this issue 11 years ago • 5 comments

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".

koenpunt avatar Apr 29 '13 12:04 koenpunt

This makes sense to me, and is how configurations work in most PHP projects/libraries.

Rican7 avatar Apr 29 '13 17:04 Rican7

Agreed. Where's that PR, @koenpunt ...? :D

al-the-x avatar May 01 '13 19:05 al-the-x

@al-the-x here is my PR: #494 (not really "soon")

koenpunt avatar Feb 10 '15 16:02 koenpunt

:+1: Keep on truckin'...

al-the-x avatar Feb 11 '15 14:02 al-the-x

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?

ajanini avatar Sep 12 '17 21:09 ajanini