Symfony2 CLI

Symfony2 CLI

As someone of you know there is pretty good feature made in Symfony2 that is worth to mention: Console interface with commands that make our life easier to generate different sort of things in our Symfony2 application and execute many useful commands with Doctrine etc.

If you want to make some custom feature in your project and that feature is not supposed to be inside your project’s user interface, than the console commands are right thing for you.

For example, I build some importers from another database to main project’s database.

It was logically to me that I should use console for importing and not UI where every user can mess up with importer.

To make custom command for Symfony2 CLI, first make new folder inside your bundle called ‘command’.

Inside of that folder we will be creating for each command one php class.
Basic command looks like this:

< ?php
namespace Surgeworks\AdminBundle\Command;
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 ImportCommand extends ContainerAwareCommand {
protected function configure() {
      ->setDescription('Import sqllite database into main DOH database')
      ->addArgument('application', InputArgument::REQUIRED, 'What application database you want to import? prayer');
protected function execute(InputInterface $input, OutputInterface $output) {
        $app = $input->getArgument('application');
        $dialog = $this->getHelperSet()->get('dialog');
        $name = $dialog->ask($output, 'Enter admin username for importing:', '');
        if ($name != 'admin') {
            die('You are not admin! Can\'t let you ruin the database! Sorry...' . "\n");
        switch ($app) {
            case 'prayers':
                $output->writeln('Importing >> prayers database ...');
                $this->ImportPrayers($input, $output);
                $output->writeln('Unknown application name. Please insert one of applications for import');

It is not complicated to make own custom command and make it work.
You just have to write methods what you want to do. Without worry, you can work with database from inside class this way:

        //get em for new tables and entities
        //We may have more entity managers defined in configuration file, so,
        //when calling entity manager write parameter with connection name to get right connection
        $em = $this->getContainer()->get('doctrine')->getEntityManager('default');
        //get connection with sqlite database
        $conn = $this->getContainer()->get('doctrine.dbal.sqlite_connection');

After that you just have to open you console and run command from there … 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Doctrine DBAL with Symfony2 Darko Goles
Darko Goles, | 4

Doctrine DBAL with Symfony2

Symfony2 writing data-fixtures Darko Goles
Darko Goles, | 3

Symfony2 writing data-fixtures

Symfony2 – Internal server error 500 Darko Goles
Darko Goles, | 1

Symfony2 – Internal server error 500


  1. Getting this error with Symfony 2.3:

    Fatal error: Call to undefined method Symfony\Component\Console\Application::getKernel() in (…)/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Command/ContainerAwareCommand.php on line 43

    1. Alex, I bumped into the same problem with Symfony 2.7, and the solution for me became rather surprising. You have probably placed you command at SomeBundle\Console\Command\YourCommand.php (as written here:
      BUT: if you need to create command “available automatically with Symfony”, you should follow THIS solution:
      As written here, you should place you Command at SomeBundle\Command\YourCommand.php (not “…\Console\Command”), and in that case your Command becomes “Automatically Registering”, and the error disappears.
      It’s a completely misleading situation, I don’t know, why the first article doesn’t have a link to the second… Anyway, I hope my suggestion will help you.

  2. thank you so much for clearing out how to call the EntityManager from a Command!. The info on the Symfony documentation is very basic and I’m still fighting with the whole concept of ‘services’ in Symfony2

  3. Really Very Nice tutorial.
    I am new to this and learning by myself. Please if possible give step by step example including entity or tell me which file or location i will put the following commands:

    $em = $this->getContainer()->get(‘doctrine’)->getEntityManager(‘default’);
    //get connection with sqlite database
    $conn = $this->getContainer()->get(‘doctrine.dbal.sqlite_connection’);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.