【问题标题】:Particular Migration Script via Controller Symfony2通过控制器 Symfony2 的特定迁移脚本
【发布时间】:2016-01-15 22:05:12
【问题描述】:

在 Symfony2 应用程序中,我正在尝试通过控制器运行迁移脚本,如下所示:

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;

class SpoolController extends Controller
{
    public function migrateAction($entity_manager = 'default')
    {
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
           'command' => 'doctrine:migrations:migrate',
           '--em' => $entity_manager,
        ));
        // You can use NullOutput() if you don't need the output
        $output = new BufferedOutput();
        $application->run($input, $output);

        // return the output, don't use if you used NullOutput()
        $content = $output->fetch();

        // return new Response(""), if you used NullOutput()
        return new Response($content);
    }
}

当我没有在命令中指定迁移脚本名称时,上面的代码正在工作。但是当我在命令中指定版本号时,如下所示:

$input = new ArrayInput(array(
       'command' => 'doctrine:migrations:migrate 20150916202248',
       '--em' => 'codes',
));

然后我收到错误:

string '[InvalidArgumentException] 命令 “教义:迁移:迁移 20150916202248”未定义。你是否 意思是其中之一?学说:迁移:状态 学说:迁移:差异 学说:迁移:执行'...(长度=880)

不能通过控制器运行特定版本的文件吗?

【问题讨论】:

    标签: symfony


    【解决方案1】:

    您是否尝试过以下方法?

    $input = new ArrayInput(array(
       'command' => 'doctrine:migrations:migrate',
       '' => '20150916202248',
       '--em' => 'codes',
    ));
    

    【讨论】:

      【解决方案2】:

      您是否尝试添加具有特定功能的参数?

      /.../
      
       $input = new ArrayInput([
             'command' => 'doctrine:migrations:migrate',
             '--em' => $entity_manager,
       ]);
      
       $input->addArgument('version', '20150916202248'); 
      
       /.../
      

      编辑

      好像是这样的解决方案:

      /.../
      
      $input = new ArrayInput([
            'command' => 'doctrine:migrations:migrate',
            'version' => '20150916202248',
            '--em' => $entity_manager,
      ]);
      
      /.../
      

      【讨论】:

      • 我收到错误Error: Call to private method Symfony\Component\Console\Input\ArrayInput::addArgument() from context 'AcmeBundle\Controller\SpoolController'
      猜你喜欢
      • 1970-01-01
      • 2018-04-06
      • 2015-06-07
      • 1970-01-01
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多