title: How to use Master/Slave doctrine2 configurations from Symfony2 Console commands
body: <p>I'm using the Doctrine2 master/slave configuration in Symfony2 to scale my application across a single master database and several read-only (replicated) slaves. </p>
  
  <p>This works perfectly from within my application and Doctrine2 sensibly uses the slave for read-only queries and the master for write operations.</p>
  
  <p>If in one of my controllers, I wrote:</p>
  
  <pre><code>$em = $this-&gt;get('doctrine')-&gt;getManager();
  var_dump($em-&gt;getConnection()-&gt;isConnectedToMaster());
  </code></pre>
  
  <p><code>isConnectedToMaster()</code> returns <code>false</code> - which I would expect since the slave is connected to by default. </p>
  
  <p>This is also the case if I run this code inside a <code>WebTestCase</code></p>
  
  <p>However if I wrote the exact same code inside a Symfony console command (ContainerAwareCommand):</p>
  
  <pre><code>$em = $this-&gt;getContainer()-&gt;get('doctrine')-&gt;getManager();
  var_dump($em-&gt;getConnection()-&gt;isConnectedToMaster());
  </code></pre>
  
  <p><code>isConnectedToMaster()</code> returns true. Which means the master is selected as the default.</p>
  
  <p>I can't find out how to stop the default connection being master from console commands. Which means if I want to run some non-critical, heavy data processing tasks from the console - they all hit the master (bad) not one of the slaves (good).</p>
  
  <p>Anyone know how I can make Doctrine2 use the slave by default from the console? Or know why it always defaults to the master?</p>
  
  <p>ANSWERED: Thanks to nifr who set me on the right course - I found that my problem was because I was using JMS\JobQueueBundle (which replaces the Application used by app/console with an altered one which must connect to the MasterSlaveConnection in a way which forces the "master" to be chosen). When I commented out the </p>
  
  <pre><code>use JMS\JobQueueBundle\Console\Application;
  </code></pre>
  
  <p>in <code>app/console</code>, the slave was correctly selected in my console test.</p>
  
  <p>Thanks</p>