Symfony/Doctrine:获取可用命令列表

Sel*_*t0r 0 php doctrine symfony

我有一个 Symfony 项目,我试图在我的控制器中获取可用命令的列表,所以我尝试执行list-command(连同--format=xml)来实现这一点。

我使用此处列出的代码从控制器运行命令,但最终出现错误“不推荐使用连接提供程序作为第一个构造函数参数已弃用”

这是代码:

    $application = new Application($kernel);
    $application->setAutoExit(false);
    $input  = new ArrayInput(['command' => 'list', '--format' => 'xml']);
    $output = new BufferedOutput();
    $application->run($input, $output);
    $content = $output->fetch();
Run Code Online (Sandbox Code Playgroud)

提到的错误发生在vendor\doctrine\dbal\lib\Doctrine\DBAL\Tools\Console\Command\RunSqlCommand.php. 这是完整的跟踪:

    $application = new Application($kernel);
    $application->setAutoExit(false);
    $input  = new ArrayInput(['command' => 'list', '--format' => 'xml']);
    $output = new BufferedOutput();
    $application->run($input, $output);
    $content = $output->fetch();
Run Code Online (Sandbox Code Playgroud)

奇怪的是,无论是否将格式设置为 XML,运行“bin/console list”(或只是“bin/console”)都不会显示错误,使用上述代码从我的控制器运行任何其他命令也可以正常工作。

我正在使用 Symfony 5.1.7 和 Doctrine-bundle 2.1,我已经尝试将学说/持久性降级到 1.x,因为 2.x 显示某些库存在一些问题,但都无济于事。

小智 6

我通过使用正确的参数在我的项目中重新声明命令来解决这个问题:

doctrine.query_sql_command:
    class: Doctrine\DBAL\Tools\Console\Command\RunSqlCommand
    arguments:
        - '@Doctrine\Bundle\DoctrineBundle\Dbal\ManagerRegistryAwareConnectionProvider'
    tags:
        - { name: console.command, command: doctrine:query:sql }
Run Code Online (Sandbox Code Playgroud)