Doctrine 2 - 如何添加自定义DBAL驱动程序? [英] Doctrine 2 - How to add custom DBAL driver?

查看:136
本文介绍了Doctrine 2 - 如何添加自定义DBAL驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加我的自定义驱动程序,而不修改Doctrine2内核中的DriverManager.php?



我已经为 pdo_dblib创建了一个DBAL驱动程序/ code>并将其放在Symfony2包中。这样做不错,但是我必须将驱动程序添加到DriverManager.php中的硬编码驱动程序列表中,否则我会收到以下异常:



异常

  [Doctrine\DBAL\DBALException ] 
给定的'driver'pdo_dblib是未知的,Doctrine目前仅支持以下驱动程序:pdo_mysql,pdo_sqlite,pdo_pgsql,pdo_oci,oci8,ibm_db2,pdo_ibm,pdo_sqlsrv
pre>

除非我修改DriverManager.php





< pre class =lang-php prettyprint-override> final class DriverManager
{
private static $ _driverMap = array(
'pdo_dblib'=>'Doctrine\\ \\ DBAL\Driver\PDODblib\Driver',//添加此行
);
}

这是我的config.yml:

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


解决方案

您实际上可以将驱动程序配置选项完全退出。



定义是driver_class选项。驱动程序只用于对默认驱动程序类进行内部查找,只要您只提供该类,则不会执行查找。



Btw:没有办法(在完整的默认设置中)在parameters.ini中定义它,您必须直接在config.yml中更改



Btw:由于另一个缺陷(驱动程序在特定区域中回退到mysql),您可能不会在配置中设置字符集,因为它将注册一个MySql事件处理程序来设置字符集。



所以我的最终的基于我的基于mssql_ *的实现的doctrine配置看起来像下面的并且没有问题的工作:

 #原则配置
doctrine:
dbal:
#driver:%database_driver%
driver_class:Doctrine\DBAL\Driver\MsSql\Driver
host:%database_host %
port:%database_port%
dbname:%database_name%
user:%database_user%
密码:%database_password%
#charset:UTF8

orm:
auto_generate_proxy_classes:%kernel.debug%
auto_mapping:真


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

解决方案

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

这篇关于Doctrine 2 - 如何添加自定义DBAL驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆