Doctrine 2 - 如何添加自定义 DBAL 驱动程序?

2023-08-19php开发问题
0

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

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17