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

xn1cxnb4  于 2022-11-09  发布在  其他
关注(0)|答案(2)|浏览(146)

我需要刷新、重置或取消设置模型;
通常,通过使用for操作,public static $k值应该改变,并且id确实改变,但是tableName模型方法只被调用一次;
tablename的值将总是1,因为这是$i的第一个值;

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;
    }

}
2j4z5cfb

2j4z5cfb1#

请尝试使用

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

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

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

qzlgjiam2#

您可以拨打

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

相关问题