I’m currently working with Yii3 and I’m trying to understand the best way to handle multiple database connections.
In Yii2 this was quite straightforward and well-documented, but in Yii3 I’m honestly struggling to find a clear and practical approach. The documentation and examples I’ve found so far are a bit confusing, and I’m not sure what the recommended pattern is.
My use case is simple: I need to work with more than one database connection within the same application (e.g., different tenants or separate data sources).
So my questions are:
Is there an official or recommended way to configure and manage multiple DB connections in Yii3?
Are there any examples or best practices available?
How should this be integrated with repositories / Active Record (if applicable)?
Any guidance, examples, or pointers to documentation would be greatly appreciated.
Hi, thanks for your reply. I actually found the answer later in the DB documentation — my bad.
Now I was wondering how Active Record management with multiple DbConnection is supposed to work in Yii3. Coming from Yii2, each Active Record had its own connection defined, while here it seems a bit more complex. From what I understand, Yii3 relies on a ConnectionProvider (or DI), but then you need to instantiate Active Records through a factory, and you also lose access to their static methods — meaning, for example, you can’t directly use methods provided by RepositoryTrait.
One idea I had was to switch the connection in the provider right before using each Active Record. However, I’m not sure if that’s safe — especially if different Active Records are used sequentially, or if an instance created with a previous connection is reused after the provider has changed. That could potentially lead to inconsistent behavior.
I suspect I might still be reasoning with a Yii2 mindset. Would the correct approach be to rely on repositories that inject the proper connection into the Active Record?
Apologies if this is a naive question — I’ve only recently started exploring Yii3. Thanks in advance to anyone who can clarify this!
In my opinion, Active Record is not the most appropriate way to handle multiple database connections since it requires a ConnectionInterface to be injected into each model.
You may wish to consider alternative methods, such as Yii DB or Doctrine ORM.
I did some reverse engineering and found out that ConnectionProvider actually accepts an array of connections. So it’s possible to configure multiple connections directly in the bootstrap definition, for example:
With this approach, you can override the db() method in your class that extends ActiveRecord and return the desired connection:
class Users extends ActiveRecord
{
use MagicPropertiesTrait;
use RepositoryTrait;
public function db(): ConnectionInterface
{
return ConnectionProvider::get('connectionB');
}
public function tableName(): string
{
return 'users';
}
}
This way, you can still use static methods like ::query() or those provided by RepositoryTrait. Internally, when static methods are called, they just instantiate an ActiveQuery passing the calling model class (e.g. Users::class). Then it simply creates an instance and calls the db() method to resolve the connection.