Yii2.0 connecting multiple databases
a project needs to connect multiple databases as needed, documenting the operational processes in the actual project. This includes the configuration of the database connection and how to generate the model file to be used in the controller.
A. Configuration
Open the database configuration file common\config\main-local.php, add DB2 under the original DB configuration item (the name can be taken by yourself, cannot be duplicated with any other database name), configure the properties of the second database to
The above configuration can complete the function of YII2 to connect multiple databases, but still need to pay attention to the actual application
two. Application
1. We create a new test table in the Huishiwang database Edu_ad
2. by generating the model through the GII, it is important to note that the database connection ID should be changed to DB2 (based on the English name of the second database library you set in mian_local)
Generate model Files ad.php
3. New Controller testcontroller.php
Print results
This is the Yii2.0 under the configuration of multi-database and practical application of the entire process, you can refer to the following. Some questions are welcome to point out.
Yii2.0 Connecting multiple databases