【问题标题】:Symfony4 - Error: "Notice: Undefined index: dbname" in AbstractOracleDriver.php while trying to create databaseSymfony4 - 错误:尝试创建数据库时 AbstractOracleDriver.php 中的“注意:未定义索引:dbname”
【发布时间】:2018-07-04 18:14:23
【问题描述】:

我使用的是 Symfony 4.0.3,我需要连接到 Oracle DB。我遵循以下说明:

https://symfony.com/doc/current/doctrine.html

我的 .env 文件

DATABASE_URL=oci8://user:pwd@xxx.xxx.xxx.xxx:1521/something

主机地址来自 tnsnames.ora -> HOST,有些东西来自 tnsnames.ora -> SERVICE_NAME

运行命令后:

php bin/console doctrine:database:create

我收到此错误:

C:\Apache24\htdocs\myapp>
2018-01-25T12:58:47+00:00 [error] Error thrown while running command 
"doctrine:d
atabase:create". Message: "Notice: Undefined index: dbname"

In AbstractOracleDriver.php line 125:

  Notice: Undefined index: dbname

我可以通过普通的 PHP 连接到数据库,但不能通过 Symfony。 dbname 来自我在 .env 文件中的连接 url。这是从 tnsnames.ora 设置的。

【问题讨论】:

  • 您能否将您的config/packages/doctrine.yaml 添加到问题中,好吗?您可以通过运行bin/console debug:container --parameters 并检查doctrine.* 参数来调试学说的当前配置。
  • 此文件未更改。我按照指示进行操作。

标签: php oracle symfony oci8


【解决方案1】:

如果您使用 SERVICE_NAME 进行连接,您可以像这样定义 DATABASE_URL:

    DATABASE_URL=oci8://user:pw@x.x.x.x:1521/?service=1&servicename=something&charset=AL32UTF8

【讨论】:

    【解决方案2】:

    在学说/dbal 版本

    这是解决此问题的 github 拉取请求: https://github.com/doctrine/dbal/pull/3297

    【讨论】:

      【解决方案3】:

      doctrine.yaml 编辑如下:

      parameters:
      # Adds a fallback DATABASE_URL if the env var is not set.
      # This allows you to run cache:warmup even if your
      # environment variables are not available yet.
      # You should not need to change this value.
      #env(DATABASE_URL): ''
      
      doctrine:
          dbal:
            default_connection:   default
            connections:
              default:
                driver:    'oci8' # ... or 'pdo_oci'
                dbname:    'dbname'
                host:      'xxx.xxx.xxx.xxx'
                port:      '1521'
                user:      'user'
                password:  'pwd'
      
      # With Symfony 3.3, remove the `resolve:` prefix
      #         url: '%env(resolve:DATABASE_URL)%' 
      orm:
          auto_generate_proxy_classes: '%kernel.debug%'
          naming_strategy: doctrine.orm.naming_strategy.underscore
          auto_mapping: true
          mappings:
              App:
                  is_bundle: false
                  type: annotation
                  dir: '%kernel.project_dir%/src/Entity'
                  prefix: 'App\Entity'
                  alias: App
      

      在命令之后 php bin/控制台原则:数据库:创建

      我有同样的错误。

      2018-01-26T06:23:31+00:00 [error] Error thrown while running command            "doctrine:d
      atabase:create". Message: "Notice: Undefined index: dbname"
      
      In AbstractOracleDriver.php line 125:
      
        Notice: Undefined index: dbname
      

      我的 dbname 与我在 SQL Developer 中的 SID 项中的相同。

      【讨论】:

        【解决方案4】:

        看起来dbname 参数不是从您的环境变量/DBAL url 中获取的。

        要快速解决这个问题,只需使用显式配置而不是 URL 来进行教义 DBAL 的配置。

        完整的参考配置可以在 symfony 文档中找到—— Doctrine DBAL Reference Configuration

        Doctrine DBAL Oracle 配置的文档可以在here找到。

        config/packages/doctrine.yaml:

        doctrine:
          # [..]
          dbal:
            default_connection:   oracle_db_1
            connections:
              oracle_db_1:
                driver:    'oci8' # ... or 'pdo_oci'
                dbname:    '<dbname>'
                host:      '<host>'
                port:      '<port>'
                user:      '<user>'
                password:  '<password>'
        

        如果可行,请使用从环境变量中读取的容器参数(即'%env(DATABASE_NAME)%')替换 dbname、主机、端口等,并将其添加到您的 .env 文件中。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-20
          • 2011-05-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-02
          相关资源
          最近更新 更多