2013-09-02 2 views
3

J'ai besoin d'un travail cron pour mon site symfony. J'ai trouvé tutoriel pour créer la commande de la console dans http://symfony.com/doc/2.1/cookbook/console/console_command.htmlGestionnaire de document dans symfony2 dans la console de commande

Mon espace de noms command.php

xxx \ WebBundle \ Command;

utilisez Symfony \ Component \ Console \ Command \ Command; utilisez Symfony \ Component \ Console \ Input \ InputArgument; utilisez Symfony \ Component \ Console \ Input \ InputInterface; utilisez Symfony \ Component \ Console \ Input \ InputOption; utilisez Symfony \ Component \ Console \ Output \ OutputInterface;

classe GreetCommand étend commande { configure la fonction protégée() {

} 

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

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

}

Quand j'appelle commande dans la console, obtenir erreur "Appel à la méthode définie xxxx \ WebBundle \ Command \ MyCommand :: getContainer() " Comment puis-je obtenir le gestionnaire de document dans la fonction d'exécution?

Répondre

6

Vous devez étend ContainerAwareCommand d'avoir accès à $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); 
    } 
} 
Questions connexes