Yii Dynamic DB connection according to user with main database

My project is based on multi-user SaaS.

I have several clients (companies), and each client has several users - all of them will use the same database layout.

Each client has its own database, so during user authentication, I want to create a main database that connects the user to the database for this user.

The structure of each database is identical ... only the data is different.

Thus, we can store different databases for different companies that are not going to mix data in the database.

The number of clients (and therefore the number of databases) is unknown when the application is written, so it is not possible to include all connections in the bootstrap script.

Now, what I want to do is dynamically change the connection to the database that is in the bootstrap, or you have the ability to dynamically create a new connection for user login. Is there a simple solution for this in Yii and still use AR, a query builder?

I saw this solution, but it didn’t work for me http://www.yiiframework.com/forum/index.php?/topic/5385-dynamic-db-connection/

This is what my configuration file looks like for a script running a single database, I want to call the main database, which controls which database the user belongs to, and the application uses any idea in Yii?

<? 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' => '127.0.0.1', 'port' => 11211, // This is the default memcached port. 'weight' => 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. ? > 
+4
source share
1 answer

Define the main database in the configuration file and use the regular AR class to connect to it to obtain credentials. Then close the connection.

Then extend the AR class, in particular the getDbConnection() method . Obtain the appropriate credentials and pass them as parameters for the new CDbConnection object.

The initial DB request for credentials will be a huge drag and drop in performance, so you really need to store them in memory using Redis or Memcached. Yii does not do this.

There are several different ways:

  • Different controllers loading different configuration files, each of which is dynamically generated or destroyed by a separate application.
  • One large database. Each record has a key to determine which client it belongs to.
  • One large database. Tables can be duplicated using the client name as part of the table name.

None of these will be easy with Yii, since it is not that Yii was intended for this.

0
source

All Articles