Yii2.0 Connecting multiple databases

Source: Internet
Author: User
Tags db2

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

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.