The default implementation will trigger an [[EVENT_BEFORE_INSERT]] event when $insert is true,
or an [[EVENT_BEFORE_UPDATE]] event if $insert is false.
When overriding this method, make sure you call the parent implementation like the following:
php
public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {
...custom code here...
return true;
} else {
return false;
}
}
public beforeSave ( boolean $insert ) : boolean | ||
$insert | boolean | whether this method called while inserting a record. If `false`, it means the method is called while updating a record. |
return | boolean | whether the insertion or updating should continue. If `false`, the insertion or updating will be cancelled. |
/** @inheritdoc */ public function beforeSave($insert) { if (!$insert) { if ($this->isAttributeChanged('Password')) { $this->Password = $this->generatePasswordHash($this->Password); } } return BaseActiveRecord::beforeSave($insert); if ($insert) { // $this->setAttribute('auth_key', \Yii::$app->security->generateRandomString()); // if (\Yii::$app instanceof \yii\web\Application) { // $this->setAttribute('registration_ip', \Yii::$app->request->userIP); // } // if (!empty($this->Password)) { // $this->setAttribute('password_hash', Password::hash($this->password)); // } } }