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