php bin/console server:run 期间 Symfony\Component\Console\Exception\LogicException

brn*_*ina 3 symfony symfony-3.3

我无法通过命令运行 symfony 本地服务器:php bin/console server:run。我收到错误: [Symfony\Component\Console\Exception\LogicException] 名为“连接”的选项已存在。

Composer.json 中的依赖项:

"require": {
        "php": "^7.0, <7.4",
        "composer/package-versions-deprecated": "^1.11",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^3.0.2",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.3.10",
        "symfony/symfony": "3.3.*",
        "twig/twig": "^1.0||^2.0"
    },
    "require-dev": {
        "doctrine/data-fixtures": "^1.3",
        "doctrine/doctrine-fixtures-bundle": "^2.3",
        "liip/functional-test-bundle": "^1.8",
        "phpunit/phpunit": "^6.3",
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
Run Code Online (Sandbox Code Playgroud)

参数.yml:

# This file is auto-generated during the composer install
parameters:
    database_host: 127.0.0.1
    database_port: 3306
    database_name: tests
    database_user: root
    database_password: password
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: ThisTokenIsNotSoSecretChangeIt
Run Code Online (Sandbox Code Playgroud)

我认为parameters.yml中的这些参数以前可以工作。我使用 mysql 和 sqlite 进行测试。

小智 8

我在 Symfony v4.2 项目中遇到了同样的问题,但没有更改我的代码库中的任何内容。

正如本期https://github.com/doctrine/dbal/issues/4565
中已经发现的那样 ,它出现在doctrine/doctrine-bundle包的某些版本中(在我的例子中为 v1.11)。来自供应商RunSqlDoctrineCommand.php添加了导致错误的第二个选项。

如果你可以更新你的doctrine/doctrine-bundle包,那可能就没问题了。就我而言,无法通过包进行更新或修复。

这种情况下我们能做什么呢?

接下来的内容更多的是一个黑客,而不是一个真正好的解决方案,所以使用它需要您自担风险!

正如官方存储库提交中的声明:https://github.com/doctrine/DoctrineBundle/commit/86d2469d6be06d55ad7b9e2f076f6942476f2e87(感谢上面问题中的人)
我从该提交中复制了新内容RunSqlDoctrineCommand.php并将其保存在我的项目中dist/RunSqlDoctrineCommand.php

更改composer.json部分scripts如下:

{
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd"
        },
        "doctrine-bugfix": [
            "cp -f dist/RunSqlDoctrineCommand.php vendor/doctrine/doctrine-bundle/Command/Proxy/RunSqlDoctrineCommand.php"
        ],
        "post-install-cmd": [
            "@auto-scripts",
            "@doctrine-bugfix"
        ],
        "post-update-cmd": [
            "@auto-scripts",
            "@doctrine-bugfix"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这只会复制并覆盖每个 .vendor 目录中的原始文件composer install/update。顺便说一句,这只适用于 unix/linux 系统。

如前所述:不是最好的解决方案,但它可以使您的项目保持良好状态。