[英]Laravel race conditions on user registration
My app's user table cannot use a unique column. 我的应用程序的用户表不能使用唯一列。 So I've done this with the Laravel validation.
因此,我通过Laravel验证完成了此操作。 The problem with this is that the "race condition" problem sometimes occurs when registering a user.
问题在于,注册用户时有时会出现“竞赛条件”问题。
I have tried several ways to fix the problem 我尝试了几种解决问题的方法
I used firstOrCreate and now firstOrNew but the problem didn't fix. 我使用了firstOrCreate,现在使用了firstOrNew,但是问题没有解决。
Although I have checked the record for non-duplicate records in the previous lines, I also checked the record's duplication status when adding records to the database, but the problem was not fixed. 尽管我已经在前几行中检查了该记录中的非重复记录,但是在将记录添加到数据库中时,我还检查了该记录的重复状态,但是该问题并未得到解决。
<?php
class RegisterController extends Controller
{
protected function validator(array $data)
{
return Validator::make($data, [
'phone' => ['required', 'digits:11', 'unique:users,phone,null,id,deleted_at,NULL', phoneRegExValidateRule()],
// other fields ...
]);
}
protected function create(array $data)
{
$user = User::firstOrNew([
'phone' => $data['phone'],
]);
if ($user->exists) {
flash()->error('error', 'duplicate user');
return redirect()->route('home');
} else {
$user->fill([
// fields ...
]);
$user->save();
}
$user->meta()->create([
// fields ...
]);
return $user;
}
}
Since the "race condition" problem occurs at the last moment and when the record is inserted into the database, it seems that I should use table locking. 由于“竞赛条件”问题发生在最后一刻,当记录插入数据库时,似乎我应该使用表锁定。 But I don't know what happens to other parts of the program that use this table when locked.
但是我不知道锁定后使用此表的程序其他部分会发生什么情况。 I also don't know how to write this code
我也不知道怎么写这段代码
用户唯一验证需要从数据库完成,而不能通过laravel完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.