通过控制器 Symfony2 运行迁移脚本

Ank*_*iiG 0 symfony

Symfony2 项目中使用了 2 个数据库,并且有一个迁移脚本放置在位置

app/DoctrineMigrations/codes/Version20150914201128.php
Run Code Online (Sandbox Code Playgroud)

此迁移不是针对默认数据库,而是针对使用的第二个数据库。

需要在用户选择时运行迁移脚本。在执行某些操作时,将打开一个弹出窗口,如果用户选择“是”,那么我只需要运行该迁移脚本。

那么在 Symfony2 中通过控制器或服务运行迁移脚本是否可能或正确的方法?

Nic*_*ich 5

看一下这个例子。您可以从控制器操作轻松触发学说迁移命令。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这个稍微改变的示例取自文档章节如何从控制器触发命令