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

StackOverflow https://stackoverflow.com/questions/4000763

  •  25-09-2019
  •  | 
  •  

문제

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() ?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top