PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, December 30, 2021

[FIXED] Yii: How to refresh or unset or reset a model?

 December 30, 2021     php, yii     No comments   

Issue

I need to refresh, reset or unset a model;

Normally, by using a for operation, the public static $k value should change, and id does change, but the tableName model method is called only once;

the value of tablename will always be 1, because that is the fisrt value of $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;
    }

}

Solution

Try using

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

to unset the attributes in a model Or you can also pass the attributes name as arguments in the method like

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


Answered By - Let me see
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing