得到這個關閉的好文章,我在命令中使用實體的發現Symfony的3
// myapplication/src/sandboxBundle/Command/TestCommand.php
// Change the namespace according to your bundle
namespace sandboxBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
// Add the Container
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
//Extend ContainerAwareCommand instead of Command
class TestCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
// the name of the command (the part after "bin/console")
->setName('app:verify-doctrine')
// the short description shown while running "php bin/console list"
->setHelp("This command allows you to print some text in the console")
// the full command description shown when running the command with
->setDescription('Prints some text into the console with given parameters.')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'My Final Symfony command',// A line
'============',// Another line
'',// Empty line
]);
$doctrine = $this->getContainer()->get('doctrine');
$em = $doctrine->getEntityManager();
// Now you can get repositories
// $usersRepo = $em->getRepository("myBundle:Users");
// $user = $usersRepo->find(1);
// outputs multiple lines to the console (adding "\n" at the end of each line)
$output->writeln("Doctrine worked, it didn't crashed :) ");
// Instead of retrieve line per line every option, you can get an array of all the providen options :
//$output->writeln(json_encode($input->getOptions()));
}
}
這裏是如果你需要任何更多的信息http://ourcodeworld.com/articles/read/239/how-to-create-and-execute-a-custom-console-command-in-symfony-3
假設你的命令是ContainerAware然後鏈接到網站$這個 - > getContainer() - >獲取( 'doctrine.orm.entity_manager'); http://symfony.com/doc/current/cookbook/console/console_command.html#getting-services-from-the-service-container – Cerad