Using multiple databases in Yii

Source: Internet
Author: User
Tags db2 yii

Background:

For scenarios where a large company has multiple subsidiaries, we often need to configure multiple sub-database (sub-databases) to store different data records.

Configuration steps:

1. The index of the master profile main.php in the application skeleton adds a ' DB2 ' =>array (//todo the code stub) to the components.

For example:

    //Application Components' Components ' =Array(        ' db ' = =Array(            ' connectionString ' = ' mysql:host=localhost;dbname=blog ', ' Emulateprepare 'true, ' username ' = ' root ', ' password ' = ', ' charset ' and ' UTF8 ', ' table ' Prefix ' = ' tbl_ ',        ), ' db2 ' =Array(            ' Class ' = ' cdbconnection ', ' connectionString ' = ' mysql:host=localhost;dbname=blog2 ', ' emulatep Repare ' =true, ' username ' = ' root ', ' password ' = ', ' charset ' and ' UTF8 ', ' table ' Prefix ' = ' tbl_ ',        ), ' user ' =Array(            //Enable cookie-based authentication' Allowautologin ' =true,        ),

2. Create a new model class in the models directory of application, since Yii uses Yii::app ()->db this cdbconnection by default, so in order to prevent the current model class from continuing to use this default is the database connection instance, You must override a name in the model
Methods of Getdbconnection:

For example:

 Public function getdbconnection ()    {        return Yii::app (),DB2;    }

3. In order to avoid some conflicting situations, please note the naming conventions of the model classes for different databases to prevent erroneous data operations from occurring.

Detailed reference: Http://www.yiiframework.com/wiki/123/multiple-database-support-in-yii

Using multiple databases in Yii

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.