通过Controller Symfony2

时间:2016-01-15 05:50:12

标签: symfony

在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]命令   " doctrine:migrations:migrate 20150916202248"没有定义。你是否   是其中之一? doctrine:migrations:status doctrine:migrations:diff   学说:迁移:执行' ...(长度= 880)

是否无法通过控制器运行特定版本文件?

2 个答案:

答案 0 :(得分:0)

您是否尝试过以下操作?

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

答案 1 :(得分:0)

您是否尝试使用特定功能添加Argument?

/.../

 $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,
]);

/.../