在Symfony2中创建命令

在Symfony2中创建命令

问题描述:

I created entity User with fields: name, password, email. Do I really need to create a service that extracts only the records of my email?($this->getContainer()->get('myservice)) Please, help me to solve this problem.

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $em = $this->getDoctrine()->getEntityManager();
        $query = $em
           ->createQuery('SELECT u.email FROM ModelBundle:User u')
        ->getResult();
        $this->getContainer()
        output->writeln($query);
    }

If i start my command, i have error

[Symfony\Component\Debug\Exception\UndefinedMethodException] Attempted to call method "getDoctrine" on class "Elastica\CoreBundle\Comman d\EmailCommand".

我创建了实体User,其中包含字段:name,password,email。 我是否真的需要创建仅提取电子邮件记录的 service code>?( $ this-> getContainer() - > get('myservice) code>) n请帮我解决这个问题。 p>

  protected function execute(InputInterface $ input,OutputInterface $ output)
 {
 $ em = $ this-> getDoctrine(  ) - > getEntityManager(); 
 $ query = $ em 
  - > createQuery('SELECT u.email FROM ModelBundle:User u')
  - > getResult(); 
 $ this->  getContainer()
 output-> writeln($ query); 
} 
  code>  pre> 
 
 

如果我启动命令,我有错误 p>

[Symfony \ Component \ Debug \ Exception \ UndefinedMethodException]尝试在类“Elastica \ CoreBundle \ Comman d \ EmailCommand”上调用方法“getDoctrine”。 code> p>

Instead of $this->getDoctrine() you should use $this->getContainer()->get('doctrine')

As $this->getDoctrine() short-cut is available only in controllers