Reputation: 55
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??
Upvotes: 0
Views: 1928
Reputation: 638
Copy the db connection file and import both connections in config (web.php):
'components' => [
...
'db' => require(__DIR__ . '/db.php'),
'db_copy' => require(__DIR__ . '/db_copy.php'),
]
Then override the db connection in the ActiveRecord which uses the copy db like this:
public static function getDb()
{
return Yii::$app->get('db_copy');
}
Upvotes: 1
Reputation: 2322
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):
// in common/config/main.php
[
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=test_db',
'username' => 'root',
'password' => '123456',
'charset' => 'utf8',
],
'modules' => [
'v1' => [
'class' => \frontend\modules\v1\Module::class,
// Increase the component configuration of the module
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=test_db_for_module',
'username' => 'root',
'password' => '111111',
'charset' => 'utf8',
],
],
],
],
]
Definition of v1 module
// in frontend/modules/Module.php
<?php
namespace frontend\modules\v1;
/**
* v1 module definition class.
*/
class Module extends \yii\base\Module
{
/**
* {@inheritdoc}
*/
public $controllerNamespace = 'frontend\modules\v1\controllers';
}
However, you have to call the db component in a special way in the module's code, for example:
// in frontend/modules/v1/controllers/TestController.php
<?php
namespace frontend\modules\v1\controllers;
/**
* Test Controller
*/
class TestController extends \yii\web\Controller {
public function actionTest()
{
\Yii::$app->modules['v1']->db->createCommand(...); // This db points to the db connection of this module configuration
// or
$this->module->db->createCommand(...) // This db points to the db connection of this module configuration
}
}
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.
Upvotes: 1