1

First of all I would like to thank you all for looking at my question. Here's my question.

I want to run three existing console commands in Symfony 2 from a controller. So I wrote three separate functions for that (see below). I managed to run 'doctrine:mapping:import' (find the code below) without any issue inside a controller. Next thing I wanted to do is generate entities based on the imported mapping files. I could not run the 'doctrine:generate:entities' command without shutting down and booting the kernel (which I think a bad idea, look at the code below). Without shutting down and booting the kernel it won't generate the entities for me. But after shutting down and booting the kernel it creates the entities for me (I am somewhat happy now). The next problem I am having is when I run the 'doctrine:generate:form' command (find the code below). When I run this code just after generating entities it say's 'Class 'THE NAME OF MY CLASS' does not exist'. This can't happen. Because I am running the form build command after generating the entities. Even I try searching for the class whether it actually there by accessing the file physically. And it is there. So I am totally stuck in here, I'd say.

Well, I know it's a lengthy question. If someone can tell what's causing generate entities to not to create entities without shutting down and booting the kernal and form builder command not to work, even the entity files are there, that would be really really appreciated. One thing I noticed though, this commands (3 functions) works fine when I run one at a time. But I want to call these 3 functions one after another. So mainly the problem occurred when I sequentially call these 3 functions.

Code to 'doctrine:mapping:import'

public function executeImportCommandAction($filter)
{
    $kernel = $this->container->get('kernel');        
    $app = new Application($kernel);
    $app->setAutoExit(false);
    $input = new \Symfony\Component\Console\Input\ArrayInput(
            array('command' => 'doctrine:mapping:import', 'bundle' => 'TESTClientBundle', '--filter'
        => $filter, 'mapping-type' => 'yml'));
    $app->doRun($input, new \Symfony\Component\Console\Output\ConsoleOutput());
}

Code to 'doctrine:generate:entities'

public function executeBuildFormCommandActions($entity)
{
    $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' => 'TESTClientBundle',
        '--no-backup' => 'true'));
    $app->doRun($input, new \Symfony\Component\Console\Output\ConsoleOutput());
}

Code to 'doctrine:generate:form'

public function executeBuildFormCommandActions($entity)
{
    #$kernel = $this->container->get('kernel');        

    $app = new Application($kernel);
    $app->setAutoExit(false);

    $input = new \Symfony\Component\Console\Input\ArrayInput(
            array('command' => 'doctrine:generate:form', 'entity' => 'TESTVClientBundle:'.$entity.''));
    $app->doRun($input, new \Symfony\Component\Console\Output\ConsoleOutput());
}

Thanks a lot in advance. Cheers!

2 Answers 2

2

Do you know there is an Process Component? http://symfony.com/doc/current/components/process.html

You can easily run a symfony command with it.

1
  • Thank for the heads up. Looks interesting. I will give it a go. Cheers! Jun 21, 2014 at 22:07
0

First of all I need to thank @vincecore to giving me the heads up regarding Symfony Process Component. I managed to workaround with Process component to achieve what I wanted to achieve. I assume kernel boot shutdown is not a proper approach even though it works well. However generate form did not work even after dealing with the kernel. This is the piece of code I found working when running all 'doctrine:mapping:import', 'doctrine:generate:entities' and 'doctrine:generate:form' console commands inside the controller. For the sake of clarity I'd like to illustrate code related to generating form.

public function executeBuildFormCommandActions($form_file)
{
    $move_to_project = 'C:/xampp5.5.11/htdocs/proj_test/';
    $commandline = "php app/console doctrine:generate:form TESTClientBundle:$form_file";
    $form_type_file = $this->get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'TEST'
            . DIRECTORY_SEPARATOR . 'ClientBundle' . DIRECTORY_SEPARATOR .
            'Form' . DIRECTORY_SEPARATOR . $form_file.'Type.php';
    if(is_file($form_type_file)){
        unlink($form_type_file);
    }        

    $process = new \Symfony\Component\Process\Process($commandline);
    $process->setWorkingDirectory($move_to_project);
    $process->run();        
    try {
        if (!$process->isSuccessful()) {                
            throw new \RuntimeException($process->getErrorOutput());
        }            
        echo $process->getOutput().'<hr/>';
    } catch (\RuntimeException $r) {
        echo $r->getMessage();
    } 
}

The good thing about this approach (Process component) is you can directly execute the command as you are executing using the command console. However without setting the working directory to project folder, this did not work at first place. That's obvious and reason behind the fact is command can't access 'app/console' anywhere outside the project folder. So I had to move the console commands inside the project folder ($process->setWorkingDirectory($move_to_project)) as executing commands within the project folder. Rest of two functions (import and generate entities) are also same, only the commands and arguments change.

Hope this helps someone who tries and find no luck when it comes to running more than one command consoles inside Symfony 2 Controller.

Cheers!

3
  • Use Process is better but how to run more than one command in a controller? This is the question. Nov 18, 2016 at 19:32
  • @VincentGuyard the above function is already inside a Controller. Take a closer look at it. Cheers :) Nov 20, 2016 at 1:27
  • When I execute a command with new Process() my php freeze and doesnt allow me to execute another command. What I have done is execute all my commands into only one using " && " between each ones. (Yes, I know, your code is inside a controller) Nov 20, 2016 at 14:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.