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

Allow DsnParser to parse driverClass via $schemeMapping #6059

Merged
merged 1 commit into from
Jun 8, 2023

Conversation

kbond
Copy link
Contributor

@kbond kbond commented Jun 7, 2023

Q A
Type feature
Fixed issues doctrine/DoctrineBundle#1673

Summary

The latest version of dbal & doctrine-bundle remove the ability to use a dsn (url param) to configure a custom driverClass.

This is a proposal to allow DsnParser::$schemeMapping to map an alias to a driverClass. With doctrine-bundle, these custom drivers could be added to the doctrine.dbal.driver_schemes config:

doctrine:
    dbal:
        driver_schemes:
            custom: My\Custom\Driver # implements Doctrine\DBAL\Driver

If this feature isn't desired, any other ideas?

@derrabus
Copy link
Member

derrabus commented Jun 8, 2023

Makes perfect sense to me. Can you please also update the documentation on the DSN parser?

Using the DSN parser
^^^^^^^^^^^^^^^^^^^^
By default, the URL scheme of the parsed DSN has to match one of DBAL's driver
names. However, it might be that you have to deal with connection strings where
you don't have control over the used scheme, e.g. in a PaaS environment. In
order to make the parser understand which driver to use e.g. for ``mysql://``
DSNs, you can configure the parser with a mapping table:
.. code-block:: php
<?php
use Doctrine\DBAL\Tools\DsnParser;
//..
$dsnParser = new DsnParser(['mysql' => 'mysqli', 'postgres' => 'pdo_pgsql']);
$connectionParams = $dsnParser
->parse('mysql://user:secret@localhost/mydb');
The DSN parser returns the connection params back to you so you can add or
modify individual parameters before passing the params to the
``DriverManager``. For example, you can add a database name if its missing in
the DSN or hardcode one if the DSN is not allowed to configure the database
name.
.. code-block:: php
<?php
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Tools\DsnParser;
//..
$connectionParams = $dsnParser->parse($myDsn);
$connectionParams['dbname'] ??= 'default_db';
$conn = DriverManager::getConnection($connectionParams);

@derrabus derrabus added this to the 3.7.0 milestone Jun 8, 2023
@kbond
Copy link
Contributor Author

kbond commented Jun 8, 2023

Docs added.

@derrabus derrabus changed the title feat: allow DsnParser to parse driverClass via $schemeMapping Allow DsnParser to parse driverClass via $schemeMapping Jun 8, 2023
@derrabus derrabus merged commit d4df00b into doctrine:3.7.x Jun 8, 2023
@derrabus
Copy link
Member

derrabus commented Jun 8, 2023

Awesome, thank you!

@kbond kbond deleted the dsn-parser-driver-class branch June 11, 2023 15:04
@MrMitch
Copy link
Contributor

MrMitch commented Oct 13, 2023

thank you for this feature, this is much appreciated when using a custom driver with doctrine-bundle, while still needing to use a url to configure the database connection

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants