Home > Back-end >  How to access the EntityManager in a Symfony Single Command Application?
How to access the EntityManager in a Symfony Single Command Application?

Time:12-04

I'm working on a Symfony Single Command Application. I'm using Doctrine to manage entities.

I created the entity configuration using the Symfony CLI and now I'm not sure how I can get access to the EM from within the run method.

Should I create a new subclass of SingleCommandApplication for this?

CodePudding user response:

If you are truly using a Single Command Application, you'll have to configure Doctrine on your own within the setCode() method. E.g. following these instructions.

(new SingleCommandApplication())
    ->setName('My Super Command') // Optional
    ->setVersion('1.0.0') // Optional
    ->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
    ->addOption('bar', null, InputOption::VALUE_REQUIRED)
    ->setCode(function (InputInterface $input, OutputInterface $output) {

        $paths = array("/path/to/entity-files");
        $isDevMode = false;

        // the connection configuration
        $dbParams = [
            'driver'   => 'pdo_mysql',
            'user'     => 'db_user',
            'password' => 'very_secret',
            'dbname'   => 'foo',
        ];

        $config = Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
        $em     = Doctrine\ORM\EntityManager::create($dbParams, $config);

        // output arguments and options
    })
    ->run();

On a single command application you do not have "bundles" as such, since you are operating outside of the Framework bundle, and generally you wouldn't have access to dependency injection either.

(If you wanted DI, you'd probably have something like this while calling the setDefaultCommand() method on the instantiated Application object, but you'd still need to instantiate the Doctrine connection "manually", even if you are doing on a different service to be injected).

  • Related