yii\db\BaseActiveRecord::beforeSave PHP Метод

beforeSave() публичный Метод

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.
Результат boolean whether the insertion or updating should continue. If `false`, the insertion or updating will be cancelled.
    public function beforeSave($insert)
    {
        $event = new ModelEvent();
        $this->trigger($insert ? self::EVENT_BEFORE_INSERT : self::EVENT_BEFORE_UPDATE, $event);
        return $event->isValid;
    }

Usage Example

Пример #1
0
 /** @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));
         //            }
     }
 }