Yii:如何更新或取消设置或重置模型?

时间:2014-01-08 21:59:37

标签: php yii

我需要刷新,重置或取消设置模型;

通常,通过使用for操作,public static $k值应该更改,而id确实会更改,但tableName模型方法只调用一次;

tablename的值始终为1,因为这是$i的fisrt值;

for($i=1;$i<=100;$i++){
VillageBuildingKXSlaveM::$server_id = 1;
VillageBuildingKXSlaveM::$k = $i;
VillageBuildingKXSlaveM::model()->findAllByAttributes(array());
}

<?php

class VillageBuildingKXSlaveM extends VillageBuildingKXM {

    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static $server_id;
    public static $slave_db;
    public static $k;

    public function getDbConnection() {
        self::$slave_db = Yii::app()->dbx;
        if (self::$slave_db instanceof CDbConnection) {
            self::$slave_db->active = false;
            $config = require(Yii::app()->getBasePath() . '/config/main.php');
            $connectionString = $config['components']['dbx']['connectionString'];
            self::$slave_db->connectionString = sprintf($connectionString, self::$server_id);
            self::$slave_db->setActive(true);
            return self::$slave_db;
        } else
            throw new CDbException(Yii::t('yii', 'Active Record requires a "db" CDbConnection application component.'));
    }

    public function tableName() {
        return 'village_building_k' . self::$k;
    }

}

2 个答案:

答案 0 :(得分:4)

尝试使用

VillageBuildingKXSlaveM::model()->unsetAttributes();

取消设置模型中的属性 或者您也可以在方法中将属性名称作为参数传递,如

VillageBuildingKXSlaveM::model()->unsetAttributes($attributes);

答案 1 :(得分:-1)

你可以打电话

VillageBuildingKXSlaveM::model()->tableName();