20

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
rooney
  • 947
  • 1
  • 7
  • 16
  • can you share your PDODblib folder as I've come across this issue aswell, also did you manage to find a way to add a custom driver without modifiying the core? – Matt Dec 19 '11 at 15:01
  • 1
    I have not found a way to add a driver without modifying the Doctrine2 core. However you can look at the Bundle at: http://github.com/trooney/PDODblibBundle – rooney Jan 04 '12 at 14:10
  • For anyone going down this path: There are unapplied commits PHP svn add lastInsertId and transaction support to the pdo_dblib driver. See http://svn.php.net/viewvc/php/php-src/trunk/ext/pdo_dblib/dblib_driver.c?view=log (rev. 300647 and 300647) – rooney Jan 19 '12 at 20:33
  • Also there is this [bundle](https://github.com/intellectsoft-uk/MssqlBundle) that you suggested from yours. – Pierre de LESPINAY Jul 11 '12 at 15:08
  • @rooney Your bundle has been very helpful but I am still struggling to set it up. I am a bit confused on how to edit the autoloader.php file to register the driver. Is this the file generated by Composer? If so, how do I get a handle of the $loader? – Pitt Nov 29 '13 at 15:28

2 Answers2

29

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
pfeigl
  • 457
  • 5
  • 12
0

You can use the option driverClass:

$connectionParams = array(
    'driverClass' => 'YOUR_CUSTOM_CLASS_DB',   
);
$entityManager = \Doctrine\ORM\EntityManager::create($connectionParams, $config);