0

There are 2 databases used in a Symfony2 project and there is a migration script which is placed at location

app/DoctrineMigrations/codes/Version20150914201128.php

This migration is not for default database, its for the second database used.

There is a need to run migration script on user selection. On some action a pop-up will open and if a user selects "Yes" then only I need to run that migration script.

So is it possible or correct way to run migration script via Controller or Service in Symfony2?

2
  • Can you more explain your issue or your objective please ? I don't understand you Commented Jan 14, 2016 at 9:30
  • @darkomen I have updated my question. Please check now. Commented Jan 14, 2016 at 9:37

2 Answers 2

4

Take a look at this example. You can trigger the doctrine migrations command easily from a controller-action.

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;

class SpoolController extends Controller
{
    public function migrateAction($entity_manager = 'default')
    {
        $kernel = $this->get('kernel');
        $application = new Application($kernel);
        $application->setAutoExit(false);

        $input = new ArrayInput(array(
           'command' => 'doctrine:migrations:migrate',
           '--em' => $entity_manager,
        ));
        // You can use NullOutput() if you don't need the output
        $output = new BufferedOutput();
        $application->run($input, $output);

        // return the output, don't use if you used NullOutput()
        $content = $output->fetch();

        // return new Response(""), if you used NullOutput()
        return new Response($content);
    }
}

This slightly changed example was taken from the docs chapter How to trigger a Command from a Controller.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I looked into this example. But as mentioned there are 2 databases so how to choose database using this?
you can use --em option of the doctrine:migrations:migrate command. Then just set up multiple entity managers ... one for each database. How to do this would be a separate question.
Just take a look at the doctrine.orm.entity_managers config directive in the DoctrineBundle's configuration reference. It's quite easy to understand.
Instead of using --em name can I pass a new entity manager created as below : $newEntityManager = \Doctrine\ORM\EntityManager::create( $new_connection, $codesConf->getConfiguration(), NULL );
Is it possible to do as above?
|
0

The Process Component could be a solution:

// .../someController

use Symfony\Component\Process\Process;

class someController extends Controller
{
   public function someAction()
   {
        $process = new Process('php app/console doctrine:migrations:migrate 20150914201128');
        $process->run();

       /.../
   }
}

3 Comments

This might work ... but booting the kernel again though you have direct access to it inside the controller is probably not a good solution.
And what about execute a migration inside a controller?
Actually how to do it inside a controller is what the question asks for. Good solution ... no. kernel.terminate event-listener would be suitable (simple) or sending to some kind of message queue (advanced).

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.