我已经设置了一个登录过程,在该过程中将生成验证码,并在成功后将其删除。但是,我要确保如果同一用户有多个验证码,则在成功登录后,删除该用户的所有记录。
这是我的代码
if ($model->validate() && $model->login()) {
//delete this verification code
$verificationCode->delete();
//delete all existing codes for user_id
VerificationCode::model()->deleteAll('user_id',$user->id);
Yii::app()->user->setReturnUrl(array('/system/admin/'));
$this->redirect(Yii::app()->user->returnUrl);
}
Run Code Online (Sandbox Code Playgroud)
但是,这似乎只是删除所有记录,而不管表中的user_id是否不同。谁能看到我要去哪里错了?
如果要删除具有指定属性的记录,最干净的方法是使用deleteAllByAttributes():
VerificationCode::model()->deleteAllByAttributes(['user_id' => $user->id]);
Run Code Online (Sandbox Code Playgroud)