YII2 cases that use multiple databases

Source: Internet
Author: User
Tags dsn yii

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 a number of databases, the document also gives a specific configuration, a practical example, is also very simple, we here with authority control for a single management library dbname2 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 '  =>  ' MySQL: Host=ip;dbname=dbname2 ',         ' username '  =>  ' username ',          ' password '  =>  ' pwd ',          ' ChArset '  =>  ' utf8 ',     ], 

no2. The second step, here we open the configuration file  main.php  , to  authmanager   option increases the configuration of the data tables associated with four permissions.

 ' AuthManager '  => [     ' class '  =>  ' Yii\rbac\dbmanager ',      ' Defaultroles '  => [' guest '],     ' itemTable '  =>   ' Dbname2.auth_item ',     ' itemchildtable '  =>  ' dbname2.auth_item_child ',      ' assignmenttable '  =>  ' dbname2.auth_assignment ',     ' Ruletable '  =>  ' dbname2.auth_rule ',], 

 vendor\yiisoft\yii2\rbac\dbmanager.php the four properties of the class, where it should be consistent to change the project configuration file directly.

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 user do with this watch and menu watch? 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

Find the file of your project config\params.php , add the following configuration item just fine

' Mdm.admin.configs ' = [' menutable ' = ' dbname2.menu ',//' usertable ' = ' dbname2.user ',],

> Note, why is this configuration item usertable shielded, this depends on the individual configuration, open file  vendor\mdmsoft\yii2-admin\components\configs.php file, see if there are usertable Property, if there is, you need to configure here, if not, you configure the trial report without error 650) this.width=650; "Src=" Http://img.baidu.com/hi/jx2/j_0067.gif " Style= "border:0px;vertical-align:middle;line-height:1.42857;" alt= "j_0067.gif"/>

One more step, the last and last step, is also an example of the explanation oh.

We take the aboveuserTableFor example, if we usegiiGenerates amodelFileUser.phpBecause the default configured table is located in thedbnameLibrary, and ouruserTable isdbname2Library in the table, so we need to modifyUser.phpThis onemodelof the filetableNamemethod, add the library name in front of the table.

public static function TableName () {return ' Dbname.user ';}

AUTH_ITEM&NBSP, and so on the table with the use of the same operation, the answer is of course no, because we use here is rbac and yii2-admin Configuration of the Rights management mechanism, the above configuration is actually we have already worked out.

What, you do not understand, that what what what what is the configuration, what what what is vendor\mdmsoft your project wood has, I guess your system of permissions control wood has done, haha, that first to see yii2 build perfect backstage and realize RBAC permission control This article, and then back to try.


YII2 cases that use multiple databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.