【问题标题】:Document manager in symfony2 in console command控制台命令中 symfony2 中的文档管理器
【发布时间】:2013-09-05 04:29:39
【问题描述】:

我的 symfony 站点需要一个 cron 任务。我找到了创建控制台命令的教程 http://symfony.com/doc/2.1/cookbook/console/console_command.html

我的命令.php

命名空间 xxx\WebBundle\Command;

使用 Symfony\Component\Console\Command\Command;采用 Symfony\组件\控制台\输入\输入参数;采用 Symfony\组件\控制台\输入\输入接口;采用 Symfony\组件\控制台\输入\输入选项;采用 Symfony\Component\Console\Output\OutputInterface;

类 GreetCommand 扩展命令 { 受保护的功能配置() {

}

protected function execute(InputInterface $input, OutputInterface $output)
{

    $em = $this->getContainer()->get('doctrine')->getManager();
    $em->getRepository('xxxWebBundle:Wishlist')->findAll();
   // $output->writeln($text);
} 

}

当我在控制台中调用命令时,出现错误“调用未定义的方法 xxxx\WebBundle\Command\MyCommand::getContainer()” 如何在执行功能中获取文档管理器?

【问题讨论】:

    标签: symfony-2.1


    【解决方案1】:

    您需要扩展ContainerAwareCommand 才能访问$this->getContainer()

    namespace xxx\WebBundle\Command;
    
    //Don't forget the use
    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    use Symfony\Component\Console\Input\InputArgument;
    use Symfony\Component\Console\Input\InputInterface;
    use Symfony\Component\Console\Input\InputOption;
    use Symfony\Component\Console\Output\OutputInterface;
    
    class GreetCommand extends ContainerAwareCommand { 
      protected function configure() {}
    
      protected function execute(InputInterface $input, OutputInterface $output)
      {
    
        $em = $this->getContainer()->get('doctrine')->getManager();
        $em->getRepository('xxxWebBundle:Wishlist')->findAll();
        // $output->writeln($text);
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-08
      • 1970-01-01
      • 2012-07-20
      • 2012-07-05
      • 2019-07-28
      相关资源
      最近更新 更多