开发者

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

开发者 https://www.devze.com 2023-01-21 19:14 出处:网络
I\'m implementing HTML sanitization for form fields, on the beforeSave() method of a model. But it doesn\'t work.

I'm implementing HTML sanitization for form fields, on the beforeSave() method of a model. But it doesn't work.

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 have to change your line

public function beforeSave()
{
  $this->anAttribute = 'somevalue';
  return true;
}


How about using afterValidate() instead of beforeSave() ?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号