Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting connection name as dbtype instead of driver property #8

Open
patrickvuarnoz opened this issue Jul 3, 2024 · 2 comments
Open

Comments

@patrickvuarnoz
Copy link

I'm in a project where I need to have multiple MySQL database connections. My setup in the database config looks like this:

return [
    'default' => _env('DB_CONNECTION', 'mysql_db_1'),
    'connections' => [
        
        'mysql_db_1' => [
            'driver' => 'mysql',
            'url' => _env('DATABASE_URL'),
            'host' => _env('DB_HOST', '127.0.0.1'),
            'port' => _env('DB_PORT', '3306'),
            'database' => _env('DB_DATABASE_1', 'forge'),
            'username' => _env('DB_USERNAME', 'forge'),
            'password' => _env('DB_PASSWORD', ''),
            'unix_socket' => _env('DB_SOCKET', ''),
            'charset' => _env('DB_CHARSET', 'utf8mb4'),
            'collation' => _env('DB_COLLATION', 'utf8mb4_unicode_ci'),
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => _env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
        
        'mysql_db_2' => [
            'driver' => 'mysql',
            'url' => _env('DATABASE_URL'),
            'host' => _env('DB_HOST', '127.0.0.1'),
            'port' => _env('DB_PORT', '3306'),
            'database' => _env('DB_DATABASE_2', 'forge'),
            'username' => _env('DB_USERNAME', 'forge'),
            'password' => _env('DB_PASSWORD', ''),
            'unix_socket' => _env('DB_SOCKET', ''),
            'charset' => _env('DB_CHARSET', 'utf8mb4'),
            'collation' => _env('DB_COLLATION', 'utf8mb4_unicode_ci'),
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => _env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
    ],
];

With the .env file having this part concerning the database:

DB_CONNECTION=mysql_db_1
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE_1=db_1
DB_DATABASE_2=db_2
DB_USERNAME=
DB_PASSWORD=
DB_CHARSET=utf8
DB_COLLATION=utf8_unicode_ci

Now when Leaf tries to connect to the database mysql_db_1 via \Leaf\Database::initDb(); it issues an error saying could not find driver in /leafs/db/src/Db/Core.php. Going through the stack trace shows that in /leafs/db/src/Db/Core.php it tries to do a connection with the following DSN:

mysql_db_1:host=127.0.0.1;dbname=db_1;port=3306;charset=utf8 

The part mysql_db_1 is the name of the connection instead of the specified driver. The problem seems to come from /leafs/mvc-core/src/Database.php where it does the following:

    public static function initDb()
    {
        if (function_exists('db')) {
            db()->connect([
                'dbUrl' => static::$config['connections'][static::$config['default']]['url'] ?? null,
                'dbtype' => static::$config['default'] ?? 'mysql',
                'charset' => static::$config['connections'][static::$config['default']]['charset'] ?? 'utf8mb4',
                'port' => static::$config['connections'][static::$config['default']]['port'] ?? '3306',
                'host' => static::$config['connections'][static::$config['default']]['host'] ?? '127.0.0.1',
                'username' => static::$config['connections'][static::$config['default']]['username'] ?? 'root',
                'password' => static::$config['connections'][static::$config['default']]['password'] ?? '',
                'dbname' => static::$config['connections'][static::$config['default']]['database'] ?? 'leaf_db',
                'collation' => static::$config['connections'][static::$config['default']]['collation'] ?? 'utf8mb4_unicode_ci',
                'prefix' => static::$config['connections'][static::$config['default']]['prefix'] ?? '',
                'unix_socket' => static::$config['connections'][static::$config['default']]['unix_socket'] ?? '',
            ]);
        }
    }

In my opinion, the line with 'dbtype' => static::$config['default'] ?? 'mysql', should actually look like this:

'dbtype' => static::$config['connections'][static::$config['default']]['driver'] ?? null,

The way it is currently implemented it is assuming that the name of the database is equal to the driver. This is also what I've seen in most of the examples where only one database is used (name always equals driver / dbtype). I can see that I can work around this by naming my first database mysql but I think that's not how it should be.

I'm using a fresh installation of Leaf in version 3.3.

@mychidarko
Copy link
Member

Can I tie this issue with the multi-db one since they're related in a sense? @patrickvuarnoz

@patrickvuarnoz
Copy link
Author

@mychidarko Of course you can tie this issue with the multi-db one (issue #9). I was initially thinking about reporting both issues in one ticket. But in my view this here is a bug that affects also non-multi-db-usage while the other issue is rather a lack of functionality that affects only multi-db-usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In progress
Development

No branches or pull requests

2 participants