Doctrine 2 - How to add custom DBAL driver?

2019-02-04 07:08发布

How can I add my custom driver without modifying DriverManager.php in the Doctrine2 core?

I have created a DBAL Driver for pdo_dblib and placed it inside a Symfony2 bundle. This works fine, however I must add my driver to a list of hard-coded drivers in DriverManager.php, otherwise I get the following exception:

Exception

[Doctrine\DBAL\DBALException]                                                                                                                                                   
The given 'driver' pdo_dblib is unknown, Doctrine currently supports only the following drivers: pdo_mysql, pdo_sqlite, pdo_pgsql, pdo_oci, oci8, ibm_db2, pdo_ibm, pdo_sqlsrv

Unless I modify DriverManager.php

final class DriverManager
{
    private static $_driverMap = array(
        'pdo_dblib' => 'Doctrine\DBAL\Driver\PDODblib\Driver', // Added this line
    );
}

Here's my config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        driver:         pdo_dblib
        driver_class:   PDODblibBundle\Doctrine\DBAL\Driver\PDODblib\Driver

1条回答
家丑人穷心不美
2楼-- · 2019-02-04 07:35

You actually can, just leave the driver configuration option completlely out.

All you need to define is the driver_class option. The driver is only used to do an internal lookup for the default driver classes, as long as you provide the class only, it will not fail doing the lookup.

Btw: There is no way (in a complete default setup) to define this in the parameters.ini, you have to change it directly inside the config.yml

Btw: due to another defect (driver falling back to mysql in on specific area), you may not set the charset in the configuration, as it will register an MySql event handler for setting the charset than.

So my final doctrine config based on my mssql_* based implementation looks like the following and works without problems:

# Doctrine Configuration
doctrine:
    dbal:
        #driver:   %database_driver%
        driver_class: Doctrine\DBAL\Driver\MsSql\Driver
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        #charset:  UTF8

    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true
查看更多
登录 后发表回答