leafsphp / mvc-core

Core functions and configurations for Leaf MVC, Leaf API and Skeleton
https://leafphp.dev/modules/mvc-core/
3 stars 4 forks source link

Selecting connection name as dbtype instead of driver property #8

Open patrickvuarnoz opened 2 months ago

patrickvuarnoz commented 2 months ago

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 commented 1 month ago

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

patrickvuarnoz commented 3 weeks ago

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

mychidarko commented 2 days ago

@patrickvuarnoz This has been fixed. Updating leafs/mvc-core should fix this issue for you. To be safe, you can update leafs/leaf as well