Skip to content
Advertisement

Yii deleteAll() records with condition

I’ve set up a log in process where a verification code is generated, and when successful, is then removed. However, i want to make sure that if there’s multiple verification codes for the same user, upon log in success, delete all records for that user.

Here’s my code

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);
}

However, this seems to just delete all the records, regardless on different user_id’s in table. Can anyone see where I’m going wrong?

Advertisement

Answer

Seems you call the function delete() in wrong way … try passing value this way

VerificationCode::model()->deleteAll('user_id = :user_id', array(':user_id' => $user->id));
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement