Answer the question
In order to leave comments, you need to log in
How in Yii2 not to crash with an error when retrying in the database?
When writing data to the database
$phone = new Users();
$phone ->name= '89404445566';
$phone ->save();
if($phone->save()) {
return 'Ок';
} else {
return 'Ошибка';
}
Answer the question
In order to leave comments, you need to log in
Good evening.
Remove the first save to the $phone ->save();
PS database
For this, in yii2, there is a validation. In this case , this built-in validator
will suit you , which will check the data transmitted by the user for uniqueness. If you try to transfer the same data, it will issue a warning.
So, for example, you can check the e-mail address for uniqueness
// в модели, в правилах валидации пишите
['email', 'unique', 'targetClass' => self::className(),
'message' => 'Извините, такой e-mail уже существует в базе данных',
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question