我找到了一个解决方法,你可以这样称呼它,或者只是一种启用命令的方法。
通过将 a 添加Command
到您自己的捆绑包之一(或专用捆绑包,取决于您),您可以简单地继承 Doctrine 命令。例如,要启用dbal:import
命令,请使用以下命令:
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 );
}
}
如您所见,我们只是将原始命令子类化。由于数据库配置由 Symfony 管理,我们需要通过容器获取实体管理器。一旦我们更新了HelperSet
我们将执行传递回父类。