Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use my Entities and Entity Managers in Symfony 2 Console Command?

I want to a few terminal commands to my Symfony2 application. I've gone through the example in the cookbook, but I couldn't find out how to access my settings, my entity manager and my entities here. In the constructor, I get the container (which should yield me access to settings and entities) using

$this->container = $this->getContainer(); 

But this call generates an error:

Fatal error: Call to a member function getKernel() on a non-object in /Users/fester/Sites/thinkblue/admintool/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php on line 38

Basically, in ContainerAwareCommand->getContainer() the call to

$this->getApplication() 

returns NULL and not an object as expected. I guess that I left some important step out, but which one? And how will I finally be able to use my settings and entities?

like image 460
Fester Bestertester Avatar asked Sep 22 '11 09:09

Fester Bestertester


People also ask

What is Symfony console?

The Console component eases the creation of beautiful and testable command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.

What is an entity in Symfony?

Well, entity is a type of object that is used to hold data. Each instance of entity holds exactly one row of targeted database table. As for the directories, Symfony2 has some expectations where to find classes - that goes for entities as well.


2 Answers

I think you should not retrieve the container in the constructor directly. Instead, retrieve it in the configure method or in the execute method. In my case, I get my entity manager just at the start of the execute method like this and everything is working fine (tested with Symfony 2.1).

protected function execute(InputInterface $input, OutputInterface $output) {     $entityManager = $this->getContainer()->get('doctrine')->getEntityManager();      // Code here } 

I think that the instantiation of the application object is not done yet when you are calling getContainer in your constructor which result in this error. The error comes from the getContainer method tyring to do:

$this->container = $this->getApplication()->getKernel()->getContainer(); 

Since getApplication is not an object yet, you get the a error saying or are calling a method getKernel on a non-object.

Update: In newer version of Symfony, getEntityManager has been deprecated (and could have been removed altogether by now). Use $entityManager = $this->getContainer()->get('doctrine')->getManager(); instead. Thanks to Chausser for pointing it.

Update 2: In Symfony 4, auto-wiring can be used to reduce amount of code needed.

Create a __constructor with a EntityManagerInterface variable. This variable will be accessible in the rest of your commands. This follows the auto-wiring Dependency Injection scheme.

class UserCommand extends ContainerAwareCommand {    private $em;     public function __construct(?string $name = null, EntityManagerInterface $em) {      parent::__construct($name);       $this->em = $em;   }     protected function configure() {      **name, desc, help code here**    }    protected function execute(InputInterface $input, OutputInterface $output) {      $this->em->getRepository('App:Table')->findAll();   } } 

Credits to @profm2 for providing the comment and the code sample.

like image 81
Matt Avatar answered Sep 28 '22 01:09

Matt


extends your command class from ContainerAwareCommand instead of Command

class YourCmdCommand extends ContainerAwareCommand 

and get entity manager like this :

$em = $this->getContainer()->get('doctrine.orm.entity_manager'); 
like image 43
stloc Avatar answered Sep 28 '22 02:09

stloc