Enabling DBAL Doctrine Commands in a Symfony Application / Console

When using a boneless doctrine with a command line that goes out of the box, two commands are available that don't seem to be available using Doctrine with Symfony and 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 any way to enable these commands in symfony app/console?

+4
source share
3 answers

I found a workaround for this, as you may call it, or maybe just a way to include commands.

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, .

+7

cli-config.php . --env php vendor/bin/doctrine. "" / Symfony.

, Doctrine <= 2.3, . .

<?php
require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';

use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';

$kernel = new AppKernel($env, $debug);
$kernel->boot();

$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
    'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()),
    'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em)
));

return $helperSet;
+3

Doctrine commands are available in symfony projects through the DoctrineBundle (since version 1.6.4 ).

Now you can run php bin/console doctrine:database:import [files].

+1
source

Source: https://habr.com/ru/post/1537609/


All Articles