I am trying to create a PBBG
application. ( where pbbg stands for persistent browser based game )
Because there will be involved multiple servers I want for each server a db.
In order to write data to a slave db, I just save a int value
into a static property, like:
public static function allocate_new_village() {
VillageSlaveM::$server_id = Yii::app()->session['user_active_world'];//1,2,3,4,5,...
$model_village_slave_m = new VillageSlaveM();
$model_village_slave_m->x = 1;
$model_village_slave_m->y = 1;
$model_village_slave_m->k = 1;
$model_village_slave_m->name = 'standard';
$model_village_slave_m->user_id = 1;
if ($model_village_slave_m->validate() && $model_village_slave_m->save()) {
// echo 1;
} else {
// echo 2;
}
}
The following example, creates a CActivedataProvider
by using the main db:
$dataProvider = new CActiveDataProvider('Village');
$this->render('index', array(
'dataProvider' => $dataProvider,
));
How do I create a CActiveDataProvider
by using a slave db and the VillageSlaveM
model?
<?php
class VillageSlaveM extends Village {
public static function model($className = __CLASS__) {
return parent::model($className);
}
public static $server_id = 1;
public static $master_db;
public function getDbConnection() {
//echo __FUNCTION__;
//die;
//echo 111;
self::$master_db = Yii::app()->{"db" . self::$server_id};
if (self::$master_db instanceof CDbConnection) {
self::$master_db->setActive(true);
return self::$master_db;
}
else
throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
}
}