【问题标题】:Launch comand, doctrine:query:sql with command ( Symfony 6 )启动命令,doctrine:query:sql with command (Symfony 6)
【发布时间】:2022-12-01 17:46:16
【问题描述】:

我想创建一个文件命令,用于使用 Symfony 命令在我的 BDD 上添加包含数据的 SQL 文件。

当我使用:

php bin/console doctrine:query:sql "$(< ./sql/zones.sql)",这个命令有效。

但是当我尝试我的文件命令时:

public function execute(InputInterface $input, OutputInterface $output)
{

   $command = $this->getApplication()->find('doctrine:query:sql');

   $files = ['$(< ./sql/zones.sql)'];

   foreach ($files as $file){

       $arguments = [
           'sql' => $file
       ];

       $greetInput = new ArrayInput($arguments);

       $command->run($greetInput, $output);
   }



}

他们返回给我 SQLSTATE[42000] 所以我认为他没有找到该文件。

我尝试更改 $file 的路径

'../../sql/zones.sql',
'./sql/zones.sql',
'"$(< ./sql/zones.sql)"',
'"$(< ../../sql/zones.sql)"'

【问题讨论】:

    标签: sql symfony doctrine symfony6


    【解决方案1】:

    我用以下方法解决了这个问题:

    public function execute(InputInterface $input, OutputInterface $output) :int
        {
    
            $command = $this->getApplication()->find('doctrine:query:sql');
            $files = ['./sql/zones.sql', './sql/status.sql', './sql/zone_prioritaire.sql', './sql/villes.sql', './sql/rememberMe.sql'];
    
            foreach ($files as $file){
    
                if (file_exists($file)){
                    $arguments = [
                        'command' => 'doctrine:query:sql',
                        'sql' => trim(file_get_contents($file))
                    ];
    
                    $greetInput = new ArrayInput($arguments);
    
                    $command->run($greetInput, $output);
                } else {
    
                    throw new Exception("File '{$file}'not found.");
                }
    
            }
    
    
            return Command::SUCCESS;
    
        }
    
    

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 2022-11-09
      • 2019-08-01
      • 2013-11-08
      相关资源
      最近更新 更多