Answer the question
In order to leave comments, you need to log in
[SOLVED] Dynamic DBs and RBAC (DbManager) Yii2?
Hello. I made a dynamic component provider according to Dmitry Eliseev's guide ( https://elisdn.ru/blog/100/active-record-dynamic-db ). The guide considers an example with connecting to different databases depending on the user. I have a slightly different format, i.e. the connection depends on the city. In general, everything is set up and everything works fine. Except for RBAC. That is, data about user roles is pulled from the main connection, because in the DbManager class, the variable public $db = 'db'; takes the db component as a basis, but it can be overridden from the description
/**
* var Connection|array|string the DB connection object or the application component ID of the DB connection.
* After the DbManager object is created, if you want to change this property, you should only assign it
* with a DB connection object.
* Starting from version 2.0.2, this can also be a configuration array for creating the object.
*/
public $db = 'db';
return [
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=cg77777_db',
'username' => 'cg77777_db',
'password' => 'asdasd',
'charset' => 'utf8',
],
'dynamicServiceLocator' => [
'class' => 'general\components\DynamicServiceLocator',
'activeCity' => [
'class' => 'general\components\ActiveCity',
'default' => 0,
],
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=cg77777_{city}',
'username' => 'cg77777_{city}',
'password' => '{password}',
'charset' => 'utf8',
],
],
],
],
];
'authManager' => [
'class' => 'general\components\myDbManager',
],
use yii\rbac\DbManager;
class myDbManager extends DbManager
{
public $db = ['dynamicServiceLocator']; <---- как сюда правильно передать текущее подключение?
}
Answer the question
In order to leave comments, you need to log in
In general, having studied all the subtleties of the theory of relativity and after long dances with a tambourine around a Voodoo doll and communicating with the world of spirits, I found the simplest solution!
use Yii;
use yii\db\Connection;
use yii\di\Instance;
use yii\rbac\DbManager;
class myDbManager extends DbManager
{
public function init()
{
parent::init();
$this->db = Instance::ensure(Yii::$app->get('dynamicServiceLocator')->get('db'), Connection::className());
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question