使用独立数据库的YII中的SaaS应用程序结构

使用独立数据库的YII中的SaaS应用程序结构,yii,Yii,我需要在YII框架中使用单独的数据库创建SaaS应用程序结构,但web中的示例使用的是单个数据库。欢迎提供任何信息或手册 谢谢您可以通过将第二个DB添加到您的main.phpex: 'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=database', 'emulatePrepare' => true, 'username' => 'use

我需要在YII框架中使用单独的数据库创建SaaS应用程序结构,但web中的示例使用的是单个数据库。欢迎提供任何信息或手册


谢谢

您可以通过将第二个DB添加到您的
main.php
ex:

    'db'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=database',
        'emulatePrepare' => true,
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8',
        'schemaCachingDuration' => 3600,
        'enableProfiling' => true,
    ),

    'db2'=>array(
        'connectionString' => 'mysql:host=localhost;dbname=database2',
        'class'=>'CDbConnection',
        'emulatePrepare' => true,
        'username' => 'username',
        'password' => 'password',
        'charset' => 'utf8',
        'schemaCachingDuration' => 3600,
        'enableProfiling' => true,
    ),
然后,您可以在要使用第二个数据库的活动记录中设置
getDbConnection()
方法。我建议用这段代码从CActiveRecord扩展一个父类,然后扩展它

它应包括:

public static $db2;

public function getDbConnection()
{
  if(self::$db2!==null)
     return self::$db2;
  else
  {
     self::$db2=Yii::app()->db2;
     if(self::$db2 instanceof CDbConnection)
     {
        self::$db2->setActive(true);
        return self::$db2;
     }
     else
        throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
  }
}