fixturesLoader = $fixturesLoader; } // phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint protected function configure() { $this ->setName('doctrine:fixtures:load') ->setDescription('Load data fixtures to your database') ->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of deleting all data from the database first.') ->addOption('group', null, InputOption::VALUE_IS_ARRAY|InputOption::VALUE_REQUIRED, 'Only load fixtures that belong to this group') ->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') ->addOption('shard', null, InputOption::VALUE_REQUIRED, 'The shard connection to use for this command.') ->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement') ->setHelp(<<%command.name% command loads data fixtures from your application: php %command.full_name% Fixtures are services that are tagged with doctrine.fixture.orm. If you want to append the fixtures instead of flushing the database first you can use the --append option: php %command.full_name% --append By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from the database. If you want to use a TRUNCATE statement instead you can use the --purge-with-truncate flag: php %command.full_name% --purge-with-truncate To execute only fixtures that live in a certain group, use: php %command.full_name% --group=group1 EOT ); } /** * @return int */ // phpcs:ignore SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint protected function execute(InputInterface $input, OutputInterface $output) { $ui = new SymfonyStyle($input, $output); $em = $this->getDoctrine()->getManager($input->getOption('em')); if (! $input->getOption('append')) { if (! $ui->confirm(sprintf('Careful, database "%s" will be purged. Do you want to continue?', $em->getConnection()->getDatabase()), ! $input->isInteractive())) { return 0; } } if ($input->getOption('shard')) { if (! $em->getConnection() instanceof PoolingShardConnection) { throw new LogicException(sprintf( 'Connection of EntityManager "%s" must implement shards configuration.', $input->getOption('em') )); } $em->getConnection()->connect($input->getOption('shard')); } $groups = $input->getOption('group'); $fixtures = $this->fixturesLoader->getFixtures($groups); if (! $fixtures) { $message = 'Could not find any fixture services to load'; if (! empty($groups)) { $message .= sprintf(' in the groups (%s)', implode(', ', $groups)); } $ui->error($message . '.'); return 1; } $purger = new ORMPurger($em); $purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); $executor = new ORMExecutor($em, $purger); $executor->setLogger(static function ($message) use ($ui) : void { $ui->text(sprintf(' > %s', $message)); }); $executor->execute($fixtures, $input->getOption('append')); return 0; } }