35

Possible Duplicate:
How can i inject dependencies to Symfony Console commands?

I want to make console application, which changes some records from the database (using Cron, every hour). How to get Doctrine ORM instance here?

In casual controller, I do this:

$this->getDoctrine();
Community
  • 1
  • 1
pamil
  • 980
  • 2
  • 10
  • 20

1 Answers1

76

If you extend from ContainerAwareCommand you should be able to get your service

$this->getContainer()->get('doctrine');

Here is similar question

Community
  • 1
  • 1
Vitalii Zurian
  • 17,858
  • 4
  • 64
  • 81
  • Thanks ;) I search in `Symfony\Component\Console` namespace, but I haven't noticed there's `Symfony\Bundle\FrameworkBundle\Console` and `Symfony\Bundle\FrameworkBundle\Command` also. – pamil Jul 29 '12 at 13:26
  • Where exactly is the 'getContainer' method defined? I can't find it on the ContainterAwareCommand.. – Luke Apr 11 '13 at 03:32
  • @Luke AFAIK this answer was related to `2.0` version. Which version are you using? – Vitalii Zurian Apr 11 '13 at 19:13
  • I'm on 2.3. There seem to be a lot of posts on SO in regards to this and none of it seems to work. In the context of 2.3, how to you get a reference to the container? I assume you need to bootstrap a Kernel first which isn't standard procedure in a console app? – Luke Sep 18 '13 at 04:13
  • @Luke could you post a link to your code? – Vitalii Zurian Sep 18 '13 at 07:05
  • This works in Symfony 3. – J.D. Pace Oct 26 '16 at 22:27