Answer the question
In order to leave comments, you need to log in
Why doesn't unique work in Yii2 model?
In general there is a console command Hello there is an action Test. when running several such processes at the same time, duplicates are obtained and the unique validation in the model does not work
public function actionTest()
{
$email = '[email protected]';
$doubleModels = Test::findOne(['email' => $email]);
if (!$doubleModels) {
$model = new Test();
$model->email = $email;
$model->save();
}
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
['email', 'unique'],
[['email'], 'string', 'max' => 255],
];
}
Answer the question
In order to leave comments, you need to log in
Try like this
public function actionTest()
{
$email = '[email protected]';
$emailExists = Test::find()->where(['email' => $email])->exists();
if (!$emailExists) {
$model = new Test();
$model->email = $email;
$model->save(false);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question