Get different value from yii find() with database value

Hi … i have some problem with my code. i want to get value from tbl_transaction_summary and update field ‘total’ and ‘balance’ with new value but new value in field ‘balance’ is not fit to my manual calculation. after i check it using var_dump($model->attributes), all of the return value is true except field ‘balance’. it was return 0, whereas the value in database is 150000.

Here my code :




public function updateSummary($coaId, $year, $month, $newTotal, $oldTotal, $isInitial, $debitCredit)

{

	$coa = Coa::model()->findByPk($coaId);

	if (!(($coa->coaType->category->debitCredit === 'Debit' && $debitCredit === 'Debit')|| ($coa->coaType->category->debitCredit === 'Credit' && $debitCredit === 'Credit')))

	{

		$oldTotal = -$oldTotal;

		$newTotal = -$newTotal;

	}

		

	$model=TransactionSummary::model()->find('coaId=:coaId and year=:year and month=:month and isInitial=:isInitial', array(':coaId'=>intval($coaId), ':year'=>$year, ':month'=>$month, ':isInitial'=>$isInitial));

	//var_dump($model->attributes);

	$model->attributes = $model->attributes;

	$model->total = $model->total - $oldTotal + $newTotal;

	$model->balance = $model->balance - $oldTotal + $newTotal;


	if ($model->save())

	{

		if ($isInitial === 'Yes')

		{

			TransactionSummary::model()->updateAll(array('balance'=>'(balance-'.$oldTotal.'+'.$newTotal.')'), 'isInitial="No"');

		}

		else

		{

			TransactionSummary::model()->updateAll(array('balance'=>'(balance-'.$oldTotal.'+'.$newTotal.')'), 'id > '.$model->id.' and isInitial="No"');

		}

		return true;

	}

	else

	{

		return false;

	}

}



but when i set manually variable $coaId in this method, the value that return was fit with database value. i already checked the value of $coaId and it was set correctly. anyone can help me ? please :(

This sounds strange, is it possible that U have some afterFind in model class, or maybe some default value that is causing this.

BTW, if U wan to update just ‘total’ and ‘balance’ you don’t need :

$model->attributes = $model->attributes; row

i already delete this . i found that something strange. when i comment $model->save(), var_dump($model->attributes) show :

array(7) { ["id"]=> string(2) "90" ["coaId"]=> string(2) "10" ["year"]=> string(4) "2014" ["month"]=> string(2) "11" ["total"]=> string(6) "100000" ["balance"]=> string(6) "150000" ["isInitial"]=> string(2) "No" }

and that value same with value in database

but when i uncomment $model->save(), var_dump($model->attributes) show :

array(7) { ["id"]=> string(2) "90" ["coaId"]=> string(2) "10" ["year"]=> string(4) "2014" ["month"]=> string(2) "11" ["total"]=> string(6) "100000" ["balance"]=> string(1) "0" ["isInitial"]=> string(2) "No" }

you can see it that the value was different on field ‘balance’ :(

i didn’t add method in model class like afterFind, afterSave, etc :( just a simple model class

here my model class




class TransactionSummary extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return TransactionSummary the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return '{{transaction_summary}}';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('coaId, year, month', 'required'),

			array('month', 'numerical', 'integerOnly'=>true),

			array('total, balance', 'numerical'),

			array('coaId', 'length', 'max'=>20),

			array('year', 'length', 'max'=>4),

			array('isInitial, balance, total', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, coaId, year, month, total, balance, isInitial', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'coa' => array(self::BELONGS_TO, 'Coa', 'coaId'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'coaId' => 'Coa',

			'year' => 'Year',

			'month' => 'Month',

			'total' => 'Total',

			'balance' => 'Balance',

			'isInitial' => 'Is Initial',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

		$criteria->compare('coaId',$this->coaId,true);

		$criteria->compare('year',$this->year,true);

		$criteria->compare('month',$this->month);

		$criteria->compare('total',$this->total);

		$criteria->compare('balance',$this->balance);

		$criteria->compare('isInitial',$this->isInitial);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}