Symfony 4: doctrine in command

后端 未结 6 1882
[愿得一人]
[愿得一人] 2021-01-05 00:44

I am using symfony 4 and I want to access a repository for an entity if I am in the Command class. There is not a function getDoctrine or something..

I

6条回答
  •  温柔的废话
    2021-01-05 01:38

    Using Symfony 4.2 here.

    use Symfony\Component\Console\Command\Command;
    use Doctrine\DBAL\Connection;
    
    class ArchiveCommand extends Command
    {
       protected static $defaultName = 'simon:test:archive';
       private $connection;
    
       public function __construct(Connection $connection)
       {
          $this->connection = $connection;
          parent::__construct();
       }
    
       /**
       * {@inheritdoc}
       */
       protected function execute(InputInterface $input, OutputInterface $output)
       {
          $this->connection->prepare('SQL HERE');
       }
    

    Works exactly as getting it from the container but is deprecated using it from the containerAwareInterface and should be injected now.

提交回复
热议问题