Yii Framework Forum: Issues related to afterSave() method - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Issues related to afterSave() method Rate Topic: -----

#1 User is offline   rahul.vit09 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-February 11

Posted 09 October 2011 - 08:26 AM

hi guys

I have to models user and expense. I wanted to change the balance field in my user model when i am creating any expense in my expense model. here is the code
protected function afterSave()
	{
		if (parent::afterSave())
			{
			$bal=User::model()->findByPk(Yii::app()->user->id);
			//Yii::trace($bal->balance);
			$bal->balance = ($bal->balance - $this->amount);
			//Yii::trace($bal->balance);
			$bal->save();	
			}	
		else
			return false;	
	}


I went through all the previous issues for afterSave(), I tried afterSave(false) as well and saveAttributes also but my User model is not getting saved. Although the trace statement give correct result. The only thing is it is not saving the changed value into the user model.
0

#2 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 09 October 2011 - 08:48 AM

Use save(false) to skip validation.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
1

#3 User is offline   ManInTheBox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 17-June 11

Posted 09 October 2011 - 08:50 AM

Why you put parent::afterSave() in condition??? Its return type is void.
http://www.yiiframew...fterSave-detail
Try this:
protected function afterSave()
{
    $bal = User::model()->findByPk(Yii::app()->user->id);
    $bal->balance = ($bal->balance - $this->amount);
    $bal->save(false);
    parent::afterSave();
}

This post has been edited by ManInTheBox: 09 October 2011 - 09:00 AM

3

#4 User is offline   rahul.vit09 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-February 11

Posted 09 October 2011 - 08:52 AM

View Posttri, on 09 October 2011 - 08:48 AM, said:

Use save(false) to skip validation.

/Tommy


I tried that nothing happened. Still not able to save. :(
0

#5 User is offline   ManInTheBox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 17-June 11

Posted 09 October 2011 - 09:03 AM

I edited my previous post, because there's really no need to check if user exists. It is currently logged in user :)
Just my coding habit to always check for proper data :)
Also I added false because you don't need to run validation. Only you are assigning new value to balance.
1

#6 User is offline   rahul.vit09 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-February 11

Posted 09 October 2011 - 09:04 AM

View PostManInTheBox, on 09 October 2011 - 08:50 AM, said:

Why you put parent::afterSave() in condition??? Its return type is void.
http://www.yiiframew...fterSave-detail
Try this:
protected function afterSave()
{
    $bal = User::model()->findByPk(Yii::app()->user->id);
    $bal->balance = ($bal->balance - $this->amount);
    $bal->save(false);
    parent::afterSave();
}



It is working now with little modification in this code i.e
$bal->save(false);
. Thanks you very much :)
0

#7 User is offline   ManInTheBox 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 75
  • Joined: 17-June 11

Posted 09 October 2011 - 09:06 AM

You're welcome! ;)
0

#8 User is offline   rahul.vit09 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 07-February 11

Posted 09 October 2011 - 09:06 AM

View PostManInTheBox, on 09 October 2011 - 09:03 AM, said:

I edited my previous post, because there's really no need to check if user exists. It is currently logged in user :)
Just my coding habit to always check for proper data :)
Also I added false because you don't need to run validation. Only you are assigning new value to balance.


Ya it is very fine. Thanks a lot :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users