title: CakePHP how to assign database connection dynamically
body: <p>I have this constant value called CLIENT_URL</p>
  
  <p>on boostrap:</p>
  
  <pre><code>$clienturl = explode('/', $_SERVER['REQUEST_URI']);
  define("CLIENT_URL",$clienturl[2]);
  </code></pre>
  
  <p>I've debugged and it gets, for example, 'client1', or 'client2', etc.
  I need to use that value when defining a configuration array, such as: </p>
  
  <p>on database.php</p>
  
  <pre><code>class DATABASE_CONFIG {
  
  var $default = array(
      'datasource' =&gt; 'Database/Mysql',
      'persistent' =&gt; false,
      'host' =&gt; 'localhost',
      'login' =&gt; '*****',
      'password' =&gt; '****',
      'database' =&gt; 'client3',
      'prefix' =&gt; ''
   );    
  
  var $clientdb = array(
      'datasource' =&gt; 'Database/Mysql',
      'persistent' =&gt; false,
      'host' =&gt; 'localhost',
      'login' =&gt; '****',
      'password' =&gt; '****',
      'database' =&gt; 'client3',
      'prefix' =&gt; ''
  );
  
  public function __construct() {
      debug('constant value: '.CLIENT_URL);
      debug($this-&gt;default['database']); 
      $this-&gt;default['database'] = CLIENT_URL; // This line does nothing !!
      //$this-&gt;clientdb['database'] = CLIENT_URL; // This lines prevent connection, error: Database connection "Mysql" is missing, or could not be created !!
      $this-&gt;default = $this-&gt;clientdb;
      debug($this-&gt;default['database']); 
  
  }
  };
  </code></pre>
  
  <p>However, database remains unchanged, it keeps initial value from above.
  Actually i can do:</p>
  
  <pre><code>$this-&gt;default['database'] = 'whatever';
  </code></pre>
  
  <p>and it won't even try to find 'whatever' database; it seems this assignment is missing some reload or something?</p>
  
  <p>After the assignment on __construct, </p>
  
  <p>I've debugged </p>
  
  <pre><code>debug($this-&gt;default['database']); 
  </code></pre>
  
  <p>Before and after the assignment and it does not change.
  It only changes doing this:</p>
  
  <pre><code>$this-&gt;clientdb['database'] = CLIENT_URL; // Instead of $this-&gt;default['database'] = CLIENT_URL;
  </code></pre>
  
  <p>But then connection fails.</p>
  
  <p>Please note the app does connect to the database given at the very beginning ('client3'), it just don't switch.
  I check database via a find, that always return same record from same database, no switch.</p>
  
  <pre><code>$options = array('conditions' =&gt; array('User.id' =&gt; 1));
  $clientArray = $this-&gt;User-&gt;find('first', $options);
  </code></pre>
  
  <p>Thank you for any pointers.</p>