在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".

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

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