we have more than one database, which we want to use in our application. So far I’ve seen only examples and google search results which are showing, how to work with one database. There are a few appropriate examples for Yii 1 (f.e. http://www.yiiframework.com/forum/index.php/topic/10649-how-to-change-db-name-on-the-fly/), but not for Yii 2. I was starting ahead with the Yii2 basic example.
I’ve tried by myself to instantiate a new instance of yii\web\Application(); but it seems, that there’s a singleton pattern contained. Also I’ve tried to find a method, which allows me to switch the configuration, but I couldn’t succeed.
That’s the way I’m instantiating my Yii2 application:
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
// create application instance and run
$config = require(__DIR__ . '/../config/web.php');
new yii\web\Application($config);
Could you please provide me hint for using multiple databases (configs) in my Yii 2 application?
My mistake was, that I’ve inserted another array dimension into the db.php file. The second db must be defined in the web.php file, inside the components array-dimension.