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

问题描述

如何在不修改 Doctrine2 核心中的 DriverManager.php 的情况下添加我的自定义驱动程序?

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

我已经为 pdo_dblib 创建了一个 DBAL 驱动程序并将它放在一个 Symfony2 包中.这工作正常,但是我必须将我的驱动程序添加到 DriverManager.php 中的硬编码驱动程序列表中,否则我会收到以下异常:

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:

例外

[DoctrineDBALDBALException]
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

除非我修改 DriverManager.php

final class DriverManager
{
    private static $_driverMap = array(
        'pdo_dblib' => 'DoctrineDBALDriverPDODblibDriver', // Added this line
    );
}

这是我的 config.yml:

# Doctrine Configuration
doctrine:
    dbal:
        driver:         pdo_dblib
        driver_class:   PDODblibBundleDoctrineDBALDriverPDODblibDriver

推荐答案

其实可以的,只需将驱动程序配置选项完全去掉即可.

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

您需要定义的只是 driver_class 选项.驱动程序只用于对默认驱动程序类进行内部查找,只要您只提供该类,查找就不会失败.

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.

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

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

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

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.

因此,基于我的基于 mssql_* 的实现,我的最终学说配置如下所示,并且可以正常工作:

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: DoctrineDBALDriverMsSqlDriver
        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 驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 03:21