【问题标题】:Symfony2 gives a ParameterNotFoundException on a database driverSymfony2 在数据库驱动程序上给出 ParameterNotFoundException
【发布时间】:2016-11-29 16:38:39
【问题描述】:

最近我用它自己的用户创建了一个额外的数据库。因此我在parameters.yml 中创建了一个额外的数据库驱动程序。据我所知,这是这种情况的标准方法。到目前为止,它有效。在我创建的一项服务中,我可以使用这个数据库驱动程序。在网站上运行代码,完全没有问题。

但当然有问题,否则我不会向你们寻求帮助。

我正在尝试通过运行以下命令来安装插件:

$ ./composer.phar require pugx/autocompleter-bundle

这会产生以下错误:

[Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
You have requested a non-existent parameter "database_driver_geo". Did you mean this: "database_driver"?

Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-update-cmd event terminated with an exception

Installation failed, reverting ./composer.json to its original content.

[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.

其他一些帖子说有关缓存的错误与文件/目录权限有关。但这似乎不是问题,因为当删除geo驱动的配置时,不会出现这种错误。

我正在运行 Symfony 2.5

[编辑:添加 parameters.yml 文件]

我的parameters.yml 看起来像这样:

# This file is auto-generated during the composer install
parameters:
    # Default database
    database_driver: pdo_mysql
    database_host: ***
    database_port: ***
    database_name: ***
    database_user: ***
    database_password: ***

    # Geo database
    database_driver_geo: pdo_mysql
    database_host_geo: ***
    database_port_geo: ***
    database_name_geo: ***
    database_user_geo: ***
    database_password_geo: ***

    mailer_transport: ***
    mailer_host: ***
    mailer_user: ***
    mailer_password: ***
    locale: ***
    secret: ***

[编辑:添加 config.yml 文件]

config.yml 文件中的教义部分:

# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:         %database_driver%
                host:           %database_host%
                port:           %database_port%
                dbname:         %database_name%
                user:           %database_user%
                password:       %database_password%
                charset:        UTF8
                mapping_types:
                    enum:       string
                    bit:        integer
                # if using pdo_sqlite as your database driver, add the path in parameters.yml
                # e.g. database_path: %kernel.root_dir%/data/data.db3
                # path:     %database_path%

            geo:
                driver:         %database_driver_geo%
                host:           %database_host_geo%
                port:           %database_port_geo%
                dbname:         %database_name_geo%
                user:           %database_user_geo%
                password:       %database_password_geo%
                charset:        UTF8
                mapping_types:
                    enum:       string
                    bit:        integer
                # if using pdo_sqlite as your database driver, add the path in parameters.yml
                # e.g. database_path: %kernel.root_dir%/data/data.db3
                # path:     %database_path%

    orm:
        default_entity_manager: default
        entity_managers:
                default:
                    connection: default
                    mappings:
                        ***CoreBundle: ~
                geo:
                    connection: geo
                    mappings:
                        ***GeoBundle: ~
        auto_generate_proxy_classes: %kernel.debug%

希望有人能帮我解决这个问题。

亲切的问候,

马尔科姆

【问题讨论】:

  • 你能发布你的parameters.yml文件吗? (隐藏敏感值,但请保留树结构)。你是否也在你的 config.yml 文件中注册了一个新的实体管理器,在教义.dbal.connections 下?
  • parameters.yml 会在您安装新的 composer 包时自动重建。您应该将参数(或至少默认值)放在parameters.yml.dist 中。它们将被复制到parameters.yml。
  • @dragoste 看起来你是对的。在我的 parameters.yml 中有比我的 .dist 版本更多的数据。但是在 config.yml symfony 中添加了这条注释:“在 parameters.yml 中添加路径”。所以他们实际上给出了错误的建议?但是密码呢?我应该将它们也添加到 .dist 版本吗?
  • parameters.yml.dist 包含默认值,例如在.dist 文件中有secret 参数具有一些默认值,但它可以在parameters.yml 在 将应用程序部署到服务器之后被覆盖。
  • @dragoste:谢谢!它解决了我的问题。如果您将您的评论作为答案发表,我会投票赞成并将其标记为答案。

标签: php symfony configuration drivers symfony-2.5


【解决方案1】:

如 cmets 中所述,parameters.yml 文件在 composer update 或 install 命令后自动重建。您可以在scripts 部分的composer.json 文件中看到:

"scripts": {
    "post-install-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        // other commands...
    ],
    "post-update-cmd": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        // other commands...
    ]
},

如果您不喜欢此功能,当然可以关闭它。但如果使用得当,它可能会很有用。

因此,当您通过 composer 安装某些软件包时,您会丢失直接放入 parameters.yml 的参数。

你应该做的是利用parameters.yml.dist文件,该文件用于构建parameters.yml。它应该提供应用程序参数值(如果它们对于每个应用程序实例都相同)或默认值,如果参数对于每个环境(产品/开发)都不同。

在您的情况下,这是第二个用例(默认值),因为每个服务器的数据库凭据都会更改。它实际上与默认数据库连接的配置完全相同。 parameters.yml.dist 包含这些参数的一些默认值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2017-05-31
    • 2012-11-15
    • 1970-01-01
    相关资源
    最近更新 更多