简体   繁体   English

在 symfony2 中为 Doctrine DBAL 配置设置 driverOptions 的正确方法

[英]Correct way to set the driverOptions for Doctrine DBAL configuration in symfony2

I have set driverOptions in the config file as mentioned in the doctrine DBAL documentation.我已经在配置文件中设置了driverOptions ,如学说 DBAL 文档中所述。

But this gives an error但这给出了一个错误

1/1 InvalidConfigurationException: Unrecognized options "driverOptions" under "doctrine.dbal.connections.pdoDevCon" 1/1 InvalidConfigurationException:“doctrine.dbal.connections.pdoDevCon”下无法识别的选项“driverOptions”

My config file is我的配置文件是

dbal:
  default_connection: pdoDevCon
  connections:
    pdoDevCon:
      driver:   %dev_database_driver%    # <
      host:     %dev_database_host%      # |
      port:     %dev_database_port%      # | Defined in
      user:     %dev_database_user%      # |
      password: %dev_database_password%  # <   
      charset:  UTF8
      driverOptions: {3: 2}
      mapping_types:
        enum: string
        set: string

orm:
    auto_generate_proxy_classes: %kernel.debug%
     pdoDevCon:
        connection: pdoDevCon
        mappings:
          AcmeDemoBundle: ~
          AcmeHelloBundle: ~ 

I am using PDO::ATTR_ERRMODE as 3 PDO::ERRMODE_EXCEPTION as 2 , it does not work even if i use the strings.我使用PDO::ATTR_ERRMODE 作为 3 PDO::ERRMODE_EXCEPTION 作为 2 ,即使我使用字符串它也不起作用。

From http://symfony.com/doc/master/reference/configuration/doctrine.html#doctrine-dbal-configuration来自http://symfony.com/doc/master/reference/configuration/doctrine.html#doctrine-dbal-configuration

DoctrineBundle supports all parameters that default Doctrine drivers accept, converted to the XML or YAML naming standards that Symfony enforces . DoctrineBundle 支持默认 Doctrine 驱动程序接受的所有参数,转换为 Symfony 强制执行的 XML 或 YAML 命名标准 See the Doctrine DBAL documentation for more information.有关更多信息,请参阅 Doctrine DBAL 文档。

There is no driverOptions in symfony yml configuration file, just options symfony yml 配置文件中没有driverOptions ,只有options

I'm not using Symfony but I was using Doctrine\\DBAL\\DriverManager::getConnection() .我没有使用 Symfony,但我使用的是Doctrine\\DBAL\\DriverManager::getConnection()

I had to side step the DriverManager and do this song and dance to specify a connection timeout ( ATTR_TIMEOUT ):我不得不侧步DriverManager并做这首歌和跳舞来指定连接超时( ATTR_TIMEOUT ):

function buildDbConn($config, $timeout) {
    $params = $config->toArray();
    $params['driverOptions'] = [
        PDO::ATTR_TIMEOUT => intval($timeout),
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    ];
    $driver = new Doctrine\DBAL\Driver\PDOMySql\Driver;
    return new Doctrine\DBAL\Connection($params, $driver);
}

I always need a pdo_mysql driver, this could be configurable.我总是需要一个 pdo_mysql 驱动程序,这可以配置。

You can set this option from the yml.您可以从 yml 设置此选项。 The thing is that you nezd to use the const value:问题是您需要使用 const 值:

The option param will be copied as driver option dont worry选项参数将被复制为驱动程序选项别担心

The int is 2整数是 2

options 2: 10选项 2:10

works for me i.对我有用。 Symfony 3.4 and 4.2 Symfony 3.4 和 4.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM