在Symfony应用程序/控制台中启用Doctrine DBAL命令 [英] Enabling Doctrine DBAL commands in Symfony app/console

查看:267
本文介绍了在Symfony应用程序/控制台中启用Doctrine DBAL命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用裸机Doctrine与开箱即用的命令行时,有两个命令可用,似乎没有使用与Symfony的Doctrine和 app / console

  dbal 
dbal:import将SQL文件直接导入到数据库。
dbal:run-sql直接从命令行执行任意SQL。是否有一种方法可以在Symfony的 app / console 中启用这些命令

解决方案

我发现了一个解决方法,你可以称之为,



通过添加命令到您自己的包之一(或专用的包,是由你),你可以简单地子类Doctrine命令。例如。以使 dbal:import 命令使用以下命令:

 命名空间Acme \Bundle\AcmeBundle\Command\Doctrine; 

使用Symfony \Component\Console\Input\InputInterface;
使用Symfony \Component\Console\Output\OutputInterface;

使用Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
使用Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;

class ImportCommand extends \Doctrine\DBAL\Tools\Console\Command\ImportCommand {

protected function execute(InputInterface $ input,OutputInterface $ output)
{
$ container = $ this-> getApplication() - > getKernel() - > getContainer();

$ doctrine = $ container-> get('doctrine');

$ em = $ doctrine-> getEntityManager();
$ db = $ em-> getConnection();

$ helperSet = $ this-> getHelperSet();
$ helperSet-> set(new ConnectionHelper($ db),'db');
$ helperSet-> set(new EntityManagerHelper($ em),'em');

parent :: execute($ input,$ output);
}

}

如您所见,子类原始命令。由于数据库配置由Symfony管理,我们需要通过容器获取实体管理器。一旦我们更新 HelperSet ,我们就将执行返回给父类。


When using bare-bone Doctrine with the command line that comes out of the box there are two commands available that don't seem to be available with using Doctrine with Symfony and the app/console:

dbal
  dbal:import    Import SQL file(s) directly to Database.
  dbal:run-sql   Executes arbitrary SQL directly from the command line.

Is there a way to enable these commands within Symfony's app/console?

解决方案

I found a workaround for this, as you may call it that, or maybe rather just a way to enable the commands.

By adding a Command to one of your own bundles (or a dedicated one, is up to you), you can simply subclass the Doctrine command. E.g. to enable the dbal:import command use the following:

namespace Acme\Bundle\AcmeBundle\Command\Doctrine;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;

class ImportCommand extends \Doctrine\DBAL\Tools\Console\Command\ImportCommand {

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $container = $this->getApplication()->getKernel()->getContainer();

        $doctrine = $container->get('doctrine');

        $em = $doctrine->getEntityManager();
        $db = $em->getConnection();

        $helperSet = $this->getHelperSet();
        $helperSet->set( new ConnectionHelper( $db ), 'db' );
        $helperSet->set( new EntityManagerHelper( $em ), 'em' );

        parent::execute( $input, $output );
    }

}

As you can see, we simply subclass the original command. Since the database configuration is managed by Symfony we need to get the entity manager through the container. Once we update the HelperSet we pass execution back to the parent class.

这篇关于在Symfony应用程序/控制台中启用Doctrine DBAL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆