Dear Friend
I have no idea about Saas.
I am not a professional in web development.
I once succeeded in switching between databases depending on the values stored in sessionin my localhost.
I hope we can apply that in your scenario.
If user has right to have separate database, when he gets logged in we are storing his id as database name.
SiteController.php
public function actionLogin()
{
$model=new LoginForm;
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login()) {
$user=User::model()->findByAttributes(array('username'=>Yii::app()->user->id));
if($user->hasOwnDatabase)
Yii::app()->user->setState('database',Yii::app()->user->id);
$this->redirect(Yii::app()->user->returnUrl); }
}
$this->render('login',array('model'=>$model));
}
[b]Create an application behavior and attach it to main application.
[/b]
applicationBehavior.php in components directory
<?php
class applicationBehavior extends CBehavior
{
private $_owner;
public function events() {
return array(
'onBeginRequest'=>'assignDb',
);
}
public function assignDb()
{
$owner=$this->_owner=$this->getOwner();
if(!$owner->user->getIsGuest && $owner->user->hasState('database'))
{
$database=Yii::app()->user->getState('database');
$owner->db->setActive(FALSE);
$owner->setComponent('db',new CDbConnection("mysql:host=localhost;dbname=$database",'root','yourpassword'));
$owner->db->setActive(TRUE);
}
}
}
?>
Attach this behavior instance as a property to main configuration file. Leave the ‘db’ component intact.
return array(
......................................
.......................................
'behaviors'=>array(
'class'=>'application.components.applicationBehavior',
),
......................................
......................................
'components'=>array(
.........................................
........................................
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=routine',//routine-default database
'emulatePrepare' => true,
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
),
.....................................................
....................................................
),
)
So when user is created, if he has right to have separate database it is stored as a boolean. Then we create a separate database on his name. After successful registration, when he login, he will use his own database on the next immediate request. Otherwise he will work on default database.
I hope this helps you a bit.
Regards.