There is a main connection to the project_database database. But besides it, there are also n- number of databases for some users of the system. Connection parameters to user databases are stored in the main database in the user table. You must connect to the user database project_database_client_123 and execute the query.
Need to create a component for each connection? And then override the getDb method of the Application to switch between them. Or is there some more elegant way?
class Connection extends \yii\db\Connection { /** * @var \yii\db\Connection[] $connections */ private $connections = []; public function getClientConnection($clientID) { if (!isset($this->connections[$clientID])) { if (!$client = Client::findOne($clientID)) { throw new Exception('Client not found'); } $connection = new \yii\db\Connection([ 'dsn' => "{$this->dsn}_client_{$clientID}", 'username' => $client->db_user, 'password' => $client->db_password, ]); $connection->open(); $this->connections[$clientID] = $connection; } return $this->connections[$clientID]; } public function closeClientConnection($clientID) { if (isset($this->connections[$clientID])) { $this->connections[$clientID]->close(); unset($this->connections[$clientID]); } } } Well, use somewhere like that.
$results = Yii::$app->db ->getClientConnection($account->client_id) ->createCommand($query, [ ':accountID' => $accountID, ]) ->queryAll(); PS These are exactly different databases, not schemas of one database.