Yii framework implements multi-database configuration and operations, and yii framework implements Database

Source: Internet
Author: User

Yii framework implements multi-database configuration and operations, and yii framework implements Database

This article describes how to configure and operate multiple databases in the Yii framework. We will share this with you for your reference. The details are as follows:

Yii is configured as a database by default, but it can easily support operations on multiple databases,

This provides the infrastructure for database separation by business. As follows:

//cms DB connection'db' => (defined('DB_CONNECTION') ? array(  'connectionString' => DB_CONNECTION,  'username' => DB_USER,  'password' => DB_PWD,  'charset' => 'utf8',  'emulatePrepare' => true,  'enableParamLogging' => true,  'schemaCachingDuration' => 3600, //cache table schema    ) : array()),//member DB connection'db_member' => (defined('DB_CONNECTION_MEMBER') ? array(  'class'=> 'CDbConnection' ,  'connectionString' => DB_CONNECTION_MEMBER,  'username' => DB_USER_MEMBER,  'password' => DB_PWD_MEMBER,  'charset' => 'utf8',  'emulatePrepare' => true,  'enableParamLogging' => true,  'schemaCachingDuration' => 3600, //cache table schema    ) : array()),

Reload the getDbConnection function in the model, for example:

class Point extends CActiveRecord {  public function getDbConnection() {     return Yii::app()->db_member;  }  ...}

If many models use db_member, You can derive a subclass from CActiveRecord: CMemberActiveRecord and overwrite getDbConnection In the subclass of this AR.

The model is derived from the CMemberActiveRecord.

A typical error in multi-database configuration is:

Object configuration must be an array containing a class element

The reason is that the class attribute is not set for other databases in non-$ db. Add this attribute and set its value to CDbConnection.

Reference link:

Http://yiihaa.com/models-and-multiple-database-connections

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.