1

I am trying to create an installation Bundle for my Symfony 2.2.3 application. Therefore I want to drop/create a database (mysql) and then create the schema via Controller Actions.

My Code:

$kernel = $this->get('kernel');
$application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
$application->setAutoExit(false);

// drop old database
$options = array('command' => 'doctrine:database:drop', '--force' => true);
$application->run(new \Symfony\Component\Console\Input\ArrayInput($options));

// create new database
$options = array('command' => 'doctrine:database:create');
$result = $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));

// check if database:create was successful, then create schema
if($result == 0) {
    $options = array('command' => 'doctrine:schema:create');
    $result = $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
}

database:drop and database:create work fine (both commands return 0), but creating the schema then fails.

However, when I comment the first 2 commands out so that only doctrine:schema:create will be executed (if clause removed, of course) and reload the page without changing anything else the database schema will be created properly.

Can anyone tell me what the problem is?

4
  • try to make space between new and \Symfony\Component\Console\Input\ArrayInput($options) line 16 Commented Sep 19, 2013 at 20:56
  • sorry that was a c&p problem, i edited my question, thanks Commented Sep 19, 2013 at 21:01
  • do you have any error? Commented Sep 19, 2013 at 21:04
  • $application->run returns 0 if everything went fine or an error code (see api.symfony.com/2.0/Symfony/Bundle/FrameworkBundle/Console/…) in my case, executing the command "doctrine:schema:create" returns 1 (and 0 the second time when everything worked) Commented Sep 19, 2013 at 21:13

1 Answer 1

1

This code works (Symfony 2.7)

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;

/**
 * @Route("/resetDB", name="adminResetDB")
 */
public function resetDBAction()
{
    $application = new Application($this->get('kernel'));
    $application->setAutoExit(false);

    // Drop old database
    $options = array('command' => 'doctrine:database:drop', '--force' => true);
    $application->run(new ArrayInput($options));

    // Make sure we close the original connection because it lost the reference to the database
    $this->getDoctrine()->getManager()->getConnection()->close();

    // Create new database
    $options = array('command' => 'doctrine:database:create');
    $application->run(new ArrayInput($options));

    // Update schema
    $options = array('command' => 'doctrine:schema:update','--force' => true);
    $application->run(new ArrayInput($options));

    // Loading Fixtures, --append option prevent confirmation message
    $options = array('command' => 'doctrine:fixtures:load','--append' => true);
    $application->run(new ArrayInput($options));

    return $this->redirect($this->generateUrl('index'));
}
Sign up to request clarification or add additional context in comments.

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.