Yii Framework Forum: Modifying a model attribute won't make it to the DB in CActiveRecord::beforeSave() - Yii Framework Forum

Jump to content

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

Modifying a model attribute won't make it to the DB in CActiveRecord::beforeSave() Rate Topic: -----

#1 User is offline   petruza 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 38
  • Joined: 12-July 10

Posted 22 October 2010 - 03:48 PM

I'm implementing HTML sanitization for form fields, on the beforeSave() method of a model. But it doesn't work.
For example:
public function beforeSave()
{
  $this->anAttribute = 'somevalue';
  var_dump( $this->anAttribute );
}

`somevalue` is actually outputted, but it never gets to the DB, so in between beforeSave() and save() the value is lost and what's saved is the original form field value.
0

#2 User is offline   tri 

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

Posted 22 October 2010 - 04:30 PM

View Postpetruza, on 22 October 2010 - 03:48 PM, said:

I'm implementing HTML sanitization for form fields, on the beforeSave() method of a model. But it doesn't work.
For example:
public function beforeSave()
{
  $this->anAttribute = 'somevalue';
  var_dump( $this->anAttribute );
}

`somevalue` is actually outputted, but it never gets to the DB, so in between beforeSave() and save() the value is lost and what's saved is the original form field value.


You should call parent::beforeSave() which will return true as default.
http://www.yiiframew...foreSave-detail

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

#3 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 23 October 2010 - 02:42 AM

beforeSave() must return a boolean value. False (or null in your case) stops saving process.

return parent::beforeSave();

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