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

只谈情不闲聊 提交于 2019-12-06 04:54:52

问题


I have set driverOptions in the config file as mentioned in the doctrine DBAL documentation.

But this gives an error

1/1 InvalidConfigurationException: Unrecognized options "driverOptions" under "doctrine.dbal.connections.pdoDevCon"

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.


回答1:


From 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. See the Doctrine DBAL documentation for more information.

There is no driverOptions in symfony yml configuration file, just options




回答2:


I'm not using Symfony but I was using Doctrine\DBAL\DriverManager::getConnection().

I had to side step the DriverManager and do this song and dance to specify a connection timeout (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.



来源:https://stackoverflow.com/questions/13298775/correct-way-to-set-the-driveroptions-for-doctrine-dbal-configuration-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!