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?
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With