public function beforeSave($insert)
{
// check if we're setting $this->password directly
// handle it by setting $this->newPassword instead
$dirtyAttributes = $this->getDirtyAttributes();
if (isset($dirtyAttributes["password"])) {
$this->newPassword = $dirtyAttributes["password"];
}
// hash new password if set
if ($this->newPassword) {
$this->password = Yii::$app->security->generatePasswordHash($this->newPassword);
}
// convert banned_at checkbox to date
if ($this->banned_at) {
$this->banned_at = gmdate("Y-m-d H:i:s");
}
// ensure fields are null so they won't get set as empty string
$nullAttributes = ["email", "username", "banned_at", "banned_reason"];
foreach ($nullAttributes as $nullAttribute) {
$this->{$nullAttribute} = $this->{$nullAttribute} ? $this->{$nullAttribute} : null;
}
return parent::beforeSave($insert);
}