title: Symfony 2: Access updated configuration from inside a command
body: <p>We are creating a command that relies on other commands to generate a new database and build out its schema. So far we have successfully gotten it to read the config.yml file, add our new connection information, and to write the file back. In the same command we are then trying to run the symfony commands to create the database and schema:update. This is where we are running into problems. We get the following error:</p>
  
  <blockquote>
    <p>[InvalidArgumentException]  Doctrine ORM Manager named "mynewdatabase"
    does not exist.</p>
  </blockquote>
  
  <p>If we run the command a second time there is no error because the updated configuration file is loaded fresh into the application. If we manually run the doctrine commands after writing to the config.yml file it also works without error.</p>
  
  <p>We are thinking that at the point in our command where we're running the database create and update commands, it's still using the current kernel's version of the config.yml/database.yml that are stored in memory. We have tried a number of different ways to reinitialize the application/kernel configuration (calling shutdown(), boot(), etc) without luck. Here's the code:</p>
  
  <pre><code>namespace Test\MyBundle\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\Input\ArrayInput;
  use Symfony\Component\Console\Output\OutputInterface;
  use Symfony\Component\Yaml\Yaml;
  
  class GeneratorCommand extends ContainerAwareCommand
  {
      protected function configure()
      {
          $this
             -&gt;setName('generate')
             -&gt;setDescription('Create a new database.')
             -&gt;addArgument('dbname', InputArgument::REQUIRED, 'The db name')
          ;
      }
  
     /*
        example: php app/console generate mynewdatabase
     */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
        //Without this, the doctrine commands will prematurely end execution
        $this-&gt;getApplication()-&gt;setAutoExit(false);
  
        //Open up app/config/config.yml
        $yaml = Yaml::parse(file_get_contents($this-&gt;getContainer()-&gt;get('kernel')-&gt;getRootDir() .'/config/config.yml'));
  
        //Take input dbname and use it to name the database
        $db_name = $input-&gt;getArgument('dbname');
  
        //Add that connection to app/config/config.yml
        $yaml['doctrine']['dbal']['connections'][$site_name] = Array('driver' =&gt; '%database_driver%', 'host' =&gt; '%database_host%', 'port' =&gt; '%database_port%', 'dbname' =&gt; $site_name, 'user' =&gt; '%database_user%', 'password' =&gt; '%database_password%', 'charset' =&gt; 'UTF8');
        $yaml['doctrine']['orm']['entity_managers'][$site_name] = Array('connection' =&gt; $site_name, 'mappings' =&gt; Array('MyCustomerBundle' =&gt; null));
  
        //Now put it back
        $new_yaml = Yaml::dump($yaml, 5);
        file_put_contents($this-&gt;getContainer()-&gt;get('kernel')-&gt;getRootDir() .'/config/config.yml', $new_yaml);
  
        /* http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command */
  
        //Set up our db create script arguments
        $args = array(
           'command'      =&gt; 'doctrine:database:create',
           '--connection'   =&gt; $site_name,
        );
        $db_create_input = new ArrayInput($args);
  
        //Run the symfony database create arguments
        $this-&gt;getApplication()-&gt;run($db_create_input, $output);
  
        //Set up our schema update script arguments
        $args = array(
           'command'   =&gt; 'doctrine:schema:update',
           '--em'      =&gt; $site_name,
           '--force'   =&gt; true
        );
        $update_schema_input = new ArrayInput($args);
  
        //Run the symfony database create command
        $this-&gt;getApplication()-&gt;run($update_schema_input, $output);
     }
  }
  </code></pre>