White Wolf Source: http://www.manks.top/article/yii2_%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8_config This article is copyright to the author, welcome reprint, but without the consent of the author must retain this paragraph of the statement , and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
About YII2 configuration operation of multiple databases, the document also gives a specific configuration, a practical example, is also very simple, we here with the authority control for a single management library dbname2, business library dbname as an example to see how to configure multiple databases.
No1. The first step, we refer to the majority of Baidu's documents to operate.
' Components ' = [' db ' = [' Class ' =' Yii\db\connection ', ' DSN ' = ' mysql:host= Ip;dbname=dbname ', ' username ' = ' username ', ' password ' = ' pwd ', ' charset ' = ' UTF8 ',], ' db2 ' = = [ ' class ' = = ' Yii\db\connection ", ' DSN ' = ' username ' = ' password ' = ' pwd ', CharSet ' + ' UTF8 ',],],
No2. The second step, where we open the configuration file main.php, adds four permissions to the configuration of the data table associated with the AuthManager option.
‘authManager‘ => [ ‘class‘ => ‘yii\rbac\DbManager‘, ‘defaultRoles‘ => [‘guest‘], ‘itemTable‘ => ‘dbname2.auth_item‘, ‘itemChildTable‘ => ‘dbname2.auth_item_child‘, ‘assignmentTable‘ => ‘dbname2.auth_assignment‘, ‘ruleTable‘ => ‘dbname2.auth_rule‘,],
In fact, this configuration is the Vendor\yiisoft\yii2\rbac\dbmanager.php class of four properties, it should be consistent to the above, directly change the project configuration file.
No3. To here actually is almost, that has the classmate question, I this has not looked, how to end? So what do you say I do with the user watch and the menu table? Don't worry, let's take a look at the third step:
This step is also very simple to drop, we have to the following simple configuration under the OK
To find your project's config\params.php file, add the following configuration item as well
‘mdm.admin.configs‘ => [ ‘menuTable‘ => ‘dbname2.menu‘, // ‘userTable‘ => ‘dbname2.user‘,],
Notice, above this why configuration item usertable block, this depends on personal configuration, open file Vendor\mdmsoft\yii2-admin\components\ configs.php file, see if there is no usertable property, if there is, you need to configure here, if not, you configure the trial report no error
One more step, the last and last step, is also an example of the explanation oh.
For example, if we use the GII to generate a model file user.php, because the default configured table is in the dbname library, and our user table is the table in the Dbname2 library, we need to modify the user.php usertable The TableName method of this model file, the table is preceded by the name of the library.
public static function tableName(){ return ‘dbname.user‘;}
There is a brain residue to ask, then that auth_item here, and so on the table with the use of the same operation, the answer is no, because we are here to use RBAC and yii2-admin configuration of the Rights management mechanism, the above configuration is actually we have worked out.
What, you do not understand, that what what what what configuration, what what what what vendor\mdmsoft your item wood has, I guess your system of permission control Wood has done well, haha.
YII2 cases that use multiple databases