V
V
Vladimir Privalov2020-08-11 20:27:56
Yii
Vladimir Privalov, 2020-08-11 20:27:56

[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';


In config
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',
                ],
            ],
        ],
    ],
];


also in the config overridden the class for authManager
'authManager' => [
    'class' => 'general\components\myDbManager',
],


and here is the class
use yii\rbac\DbManager;

class myDbManager extends DbManager
{
    public $db = ['dynamicServiceLocator']; <---- как сюда правильно передать текущее подключение?
}


But I get the error "Setting unknown property: yii\db\Connection::0". Has anyone implemented this functionality? How to speed up the right connection to authManager?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Privalov, 2020-08-13
@wprivalov

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());
    }

}

All it took was to override the init() DbManager'a method.
Let's go guys :D

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question