Php 如何在Symfony中使用控制台中的实体命令

Php 如何在Symfony中使用控制台中的实体命令,php,symfony,Php,Symfony,如何在控制台命令中使用条令和实体?在控制器中,我只执行$this->getdoctor…,但在命令中,我发现我必须使用container,$this->getContainer()->getdoctor(),但这会生成控制台错误: 无法检索容器,因为尚未设置应用程序实例 谷歌没有帮我 我从一篇关于在symfony3的命令中使用实体的好文章中得到了这一点 // myapplication/src/sandboxBundle/Command/TestCommand.php // Change the

如何在控制台命令中使用条令和实体?在控制器中,我只执行$this->getdoctor…,但在命令中,我发现我必须使用container,$this->getContainer()->getdoctor(),但这会生成控制台错误:

无法检索容器,因为尚未设置应用程序实例

谷歌没有帮我


我从一篇关于在symfony3的命令中使用实体的好文章中得到了这一点

// 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()));
    }
}

这里是指向该站点的链接,以备您需要更多信息

我从一篇关于在Symfony 3中使用Entity的in命令的文章中找到了这篇文章

// 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()));
    }
}

这里是指向站点的链接,以防您需要更多信息

假设您的命令是ContainerWare,那么$this->getContainer()->get('doctrine.orm.entity_manager');假设您的命令是containerware,那么$this->getContainer()->get('doctrine.orm.entity_manager');