3

I want create ContainerAwareCommand in Symfony2 for print email from my database. I have another class, which consist my data. I only start, but what i my 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 does not exist

2 Answers 2

1

Follows the documentation.

In short, add execute() method 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*/);
    }
Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to get the data without creating a service?
If I didn't understand what you meant, I'm sure others won't either. Please explain.
@John Cartwright i update code. I want get all email from my db, but i can't create service for this
1

May be you're doing something wrong, check class name, directory name, etc. From the doc:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.