Question

I need a cron job for my symfony site. I have found tutorial to create console command in http://symfony.com/doc/2.1/cookbook/console/console_command.html

My command.php

namespace xxx\WebBundle\Command;

use Symfony\Component\Console\Command\Command; 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 Command { protected function configure() {

}

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

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

}

When I call command in console, getting error " Call to undefined method xxxx\WebBundle\Command\MyCommand::getContainer() " How can I get the document manger in execute function?

Was it helpful?

Solution

You need to extends ContainerAwareCommand to have access to $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);
  } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top