For some cases when the database connection cannot be obtained via dependency injection, you can use the ConnectionProvider
class to define the DB connection and then get it where needed.
Add the following code to the configuration file, for example, in config/common/bootstrap.php:
use Psr\Container\ContainerInterface;
use Yiisoft\Db\Connection\ConnectionProvider;
use Yiisoft\Db\Connection\ConnectionInterface;
return [
static function (ContainerInterface $container): void {
ConnectionProvider::set($container->get(ConnectionInterface::class));
}
];You can get the defined connection using the ConnectionProvider::get() method:
use Yiisoft\Db\Connection\ConnectionProvider;
$db = ConnectionProvider::get(); // Gets the default connection
$db2 = ConnectionProvider::get('db2'); // Gets the connection by name