mirror of
https://github.com/elyby/accounts.git
synced 2025-05-31 14:11:46 +05:30
Логика генерации значения первичного ключа для строк вынесена в поведение
This commit is contained in:
@@ -3,6 +3,7 @@ namespace common\models;
|
||||
|
||||
use common\behaviors\DataBehavior;
|
||||
use common\behaviors\EmailActivationExpirationBehavior;
|
||||
use common\behaviors\PrimaryKeyValueBehavior;
|
||||
use common\components\UserFriendlyRandomKey;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\behaviors\TimestampBehavior;
|
||||
@@ -42,6 +43,12 @@ class EmailActivation extends ActiveRecord {
|
||||
'class' => TimestampBehavior::class,
|
||||
'updatedAtAttribute' => false,
|
||||
],
|
||||
[
|
||||
'class' => PrimaryKeyValueBehavior::class,
|
||||
'value' => function() {
|
||||
return UserFriendlyRandomKey::make();
|
||||
},
|
||||
],
|
||||
'expirationBehavior' => [
|
||||
'class' => EmailActivationExpirationBehavior::class,
|
||||
'repeatTimeout' => 5 * 60, // 5m
|
||||
@@ -84,26 +91,4 @@ class EmailActivation extends ActiveRecord {
|
||||
];
|
||||
}
|
||||
|
||||
public function beforeSave($insert) {
|
||||
if (!parent::beforeSave($insert)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->key === null) {
|
||||
do {
|
||||
$this->key = $this->generateKey();
|
||||
} while ($this->isKeyExists($this->key));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function generateKey() : string {
|
||||
return UserFriendlyRandomKey::make();
|
||||
}
|
||||
|
||||
protected function isKeyExists(string $key) : bool {
|
||||
return self::find()->andWhere(['key' => $key])->exists();
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user