1

I need to call a console command from a controller to generate new entities. Here is the code I have so far:

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Application;

public function newAction()
{

    $kernel = $this->container->get('kernel');
    $kernel->shutdown();
    $kernel->boot();
    $app = new Application($kernel);
    $app->setAutoExit(false);

    $input = new \Symfony\Component\Console\Input\ArrayInput(array('command' =>  
        'doctrine:generate:entities', 'name'  => 'AdminBundle','-- o-backup' => 'true'));

    $app->doRun($input, new \Symfony\Component\Console\Output\ConsoleOutput());
}

But this is the error I got:

There are no commands defined in the "doctrine:generate" namespace.

I hope someone can help me to fix the error.

acab
  • 11
  • 3
  • 1
    I'm not sure why you would want to use the kernel here. It's pretty simple to call the command, http://stackoverflow.com/questions/10497567/how-can-i-run-symfony-2-run-command-from-controller – clouddreams Nov 29 '14 at 03:35
  • I am developing a project that allows users to define their own data schema. Thus, I created the first step where users can actually create new tables by defining fields and data type. The problem is how to allow users to insert and modify data in the new table. For that reason, I want to generate the entity based on the new table from a controller. – acab Nov 29 '14 at 03:46

1 Answers1

-1

This is how I did it for an old version of Symfony - 2.0:

<?php
namespace Admin\MyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

use Admin\MyBundle\Command;

/**
 * @author Nataliya Naydenova
 *
 * @Route("/cron-manager")
 */
class CronManagerController extends Controller 
{ 

    /**
     * Manually run cron jobs
     *
     * @param $commandName as defined in MyCommand.php
     */
    private function runCommandMannually($commandName)
    {

        $text = 'Starting ...';

        ini_set('max_execution_time', 600);

        $command = new Command\MyCommand();
        $command->setContainer($this->container);

        $arguments = array(
            'command' => 'my:command',
            'name'    => $commandName,
            '--env'  => 'local',
        );

        $input = new ArgvInput($arguments);
        $output = new ConsoleOutput();

        $returnCode = $command->run($input, $output);

        if($returnCode == 0) {
            $text .= 'successfully loaded!';
        } else {
            $text .= 'error!: ' . $returnCode;
        }

        $this->get('session')->setFlash('message', $text);
        return $this->redirect($this->generateUrl('cron_manager'));
    }

    /**
     * Pull first cron
     *
     * @Route("/product-image", name="cron_manager_product_image")
     */
    public function productImageAction()
    {
        self::runCommandMannually('product_image_update');
    }

    /**
     * Pull second cron
     *
     * @Route("/product-info", name="cron_manager_product_info")
     */
    public function productInfoAction()
    {
        self::runCommandMannually('product_info_update');
    }
}


Exlpanation

The commands I want to run via this controller are located in the Admin/MyBundle/Command/MyCommand.php class.

Because they take quite some time to execute I also increase the maximal execution time via ini_set('max_execution_time', 600);

The next thing to do is set the container for the MyCommand() object and then pass all necessary parameters to the input: the command itself, its name and the environment.

The $command->run() method then runs and returns either 0 on success or a warning on failure. Depending on it I structure my flash message and redirect to the manager page from where I control those crons.

Nat Naydenova
  • 771
  • 2
  • 12
  • 30