Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A case where yii2 uses multiple databases

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Author: White Wolf provenance: http://www.manks.top/article/yii2_%E5%88%86%E5%BA%93%E5%88%86%E8%A1%A8_config the copyright of this article belongs to the author, welcome to reprint, but this statement must be retained without the author's consent, and the link to the original text must be given in a prominent position on the article page, otherwise, we reserve the right to pursue legal liability.

With regard to yii2 configuration, multiple databases are operated. The specific configuration is also given above. A practical example is also very simple. Here, we take permission control as a single management library dbname2 and business library dbname as an example to see how to configure multiple databases.

No1. In the first step, we refer to most of Baidu's documents for operation.

'components' = > [' db' = > ['class' = >' yii\ db\ Connection', 'dsn' = >' mysql:host=ip;dbname=dbname', 'username' = >' username', 'password' = >' pwd', 'charset' = >' utf8',], 'db2' = > [' class' = > 'yii\ db\ Connection',' dsn' = > 'mysql:host=ip' Dbname=dbname2', 'username' = >' username', 'password' = >' pwd', 'charset' = >' utf8',],]

No2. In the second step, we open the configuration file main.php and add 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, what is configured here is the four properties of the vendor\ yiisoft\ yii2\ rbac\ DbManager.php class, which should be consistent with the above and change the project configuration file directly.

No3. As a matter of fact, it's almost done here. There are questions from my classmates. I haven't seen it yet. How come it's over? What do you say about my user and menu watches? Don't worry, let's look at the third step:

This step is also very simple. Let's simply configure ok as follows.

Find the config\ params.php file for your project and add the following configuration items

'mdm.admin.configs' = > [' menuTable' = > 'dbname2.menu', / /' userTable' = > 'dbname2.user',]

Note that the above configuration item userTable is blocked. This depends on the individual configuration. Open the file vendor\ mdmsoft\ yii2-admin\ components\ Configs.php to see if there is any userTable attribute. If so, you need to configure it. If not, try to report an error.

There is still one step left, and the last step is also an example.

We take the above userTable as an example, if we use gii to generate a model file User.php, because the default configuration table is located in the dbname library, and our user table is a table in the dbname2 library, so we need to modify the tableName method of the model file User.php, with the library name in front of the table.

Public static function tableName () {return 'dbname.user';}

There are mentally handicapped fans to ask, then whether the auth_item and other tables here need to operate in this way, the answer is of course No, because we use the permission management mechanism of rbac and yii2-admin configuration here. In fact, we have already worked out the above configuration items.

What, you don't understand, what configuration, what vendor\ mdmsoft your project does not have, I guess the permission control of your system has not been done well, , first take a look at the article here where yii2 builds a perfect background and implements rbac permission control, and then go back and try again.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report