我正在尝试创建一个PBBG
应用程序。(其中 pbbg 代表基于浏览器的持久游戏)
因为将涉及多台服务器,我希望每个服务器都有一个 db。
为了将数据写入从数据库,我只需将 a 保存int value
到静态属性中,例如:
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;
}
}
以下示例CActivedataProvider
使用主数据库创建一个:
$dataProvider = new CActiveDataProvider('Village');
$this->render('index', array(
'dataProvider' => $dataProvider,
));
如何CActiveDataProvider
使用从数据库和VillageSlaveM
模型创建一个?
<?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.'));
}
}