I want to combine two applications. One of these as module. There are two different databases. The Module use another database as the base appliaction.
I have created the db connection, which is a component in config.php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=test_db',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
And I have created an ModuleActiveRecord Class which overwrites the Active Record function getDB():
class ModuleActiveRecord extends ActiveRecord
{
public static function getDb()
{
$db = Yii::$app->controller->module->db;
return Yii::createComponent($db);
}
I get the error message: The table does not exist. Anyone an idea??
You can add multiple database connections like the other answers, for example: db/db_for_module.
You can also configure the module like I do(Example using Yii2 advanced template):
Definition of v1 module
However, you have to call the db component in a special way in the module's code, for example:
The benefits of doing this:
Note: This is just a way to override the application default components in the module for reference. I have not practiced this method, but I have tested this is feasible.