Create ContainerAwareCommand in Symfony2 to Print Data

I want to create a ContainerAwareCommand in Symfony2 to print email from my database. I have another class that consists of my data. I'm just getting started, but what am I going to do next?

protected function configure()
{
    $this
        ->setName('demo:email:get')
        ->setDescription('Print all emails form db');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
    $service = $this->getContainer()->get('fos_elastica.finder.site.user.email');
    $output->writeln('<p>%s</p>', $service);
}

      

But in my console my command doesn't exist

+3


source to share


2 answers


Performs documentation .

In short, add a method execute()

to the class, get your service from the container, then display the data.



   protected function execute(InputInterface $input, OutputInterface $output)
    {
        $service = $this->getContainer()->get('your_service.database_connection');

        $email = // work with $service

        $output->writeln(/*here you render the data*/);
    }

      

+1


source


You may be doing something wrong, checking the class name, directory name, etc. From doc :



To make console commands available automatically with Symfony, create a Command directory in your package and create a PHP file suffix with Command.php for each command you want to provide. For example, if you want to extend the AppBundle to greet you command line, create GreetCommand.php

+1


source







All Articles