title: Yii Dynamic DB Connection according to user with master database
body: <p>My project is on the basis of multi-tenent SaaS.</p>
  
  <p>I have multiple clients (companies) and each client has multiple users - they all will use the same database layout.</p>
  
  <p>Each client has their own database, so during user authentication, I want to Build a master database that associates the user with a company database for that user.</p>
  
  <p>The structure of each database is identical... only the data is different.</p>
  
  <p>So that we can keep the different database for the different company, that will not going to mix in data in database.</p>
  
  <p>The number of clients (and therefor the number of databases) is unknown when the application is written, so it is not possible to include all the connections in the bootstrap script.</p>
  
  <p>Now, what I want to do is, dynamically alter the DB connection that is in the bootstrap or have the ability to dynamically create a new connection for the user signing in. Is there a simple solution for this in Yii and still use AR , query builder ?</p>
  
  <p>I saw this solution but not working for me <a href="http://www.yiiframework.com/forum/index.php?/topic/5385-dynamic-db-connection/" rel="nofollow">http://www.yiiframework.com/forum/index.php?/topic/5385-dynamic-db-connection/</a></p>
  
  <p>This is how my config file looks today for the script running one database, i want to call a master database that controls which database the user belongs to and the app uses in Yii, any idea?</p>
  
  <pre><code>&lt;? php
  
   $language = 'en'; 
  
    $currencyBaseCode = 'USD';
  
   $theme = 'default';
  
   $connectionString = 'mysql:host=localhost;port=3306;dbname=master';
  
   $username = 'root';
  
   $password = 'YOUR PASS';
  
   $memcacheServers = array( // An empty array means memcache is not used.
  
   array(
  
  'host' =&gt; '127.0.0.1',
  
  'port' =&gt; 11211, // This is the default memcached port.
  
  
   'weight' =&gt; 100,
  
    ),
  
    );
  
    $adminEmail = 'EMAIL ADDRESS';
  
    $installed = true; // Set to true by the installation process.
  
    $maintenanceMode = false; // Set to true during upgrade process or other maintenance tasks.
  
    $instanceConfig = array(); //Set any parameters you want to have merged into configuration array.
  
    //@see CustomManagement
  
    $instanceConfig['components']['request']['hostInfo'] = 'website url';
  
    $instanceConfig['components']['request']['scriptUrl'] = '/app/index.php';
  
    $urlManager = array (); // Set any parameters you want to customize url manager. 
    ? &gt;
  </code></pre>