0

我正在尝试创建一个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.'));
    }

}
4

1 回答 1

0

出于某种原因,一开始它不起作用,现在它起作用了:

VillageSlaveM::$server_id = Yii::app()->session['user_active_world'];
$dataProvider = new CActiveDataProvider('VillageSlaveM');
$this->render('index', array(
    'dataProvider' => $dataProvider,
));
于 2013-09-30T20:56:14.160 回答