The situation is: we sent mails to users, and they need to activate theirs registration. The activation URL include the user’s email address and the user’s verificationkey (random generated). After registration and before activation users are in the ‘registered’ usergroup. After activation they will be in the ‘confirmed’ group.
Here is the code:
public function actionActivation($email, $key) {
$model=User::model()->findByAttributes(array('email'=>urldecode($email), 'verificationkey'=>$key, 'usergroup'=>'registered'));
if($model!==null) {
$model->usergroup = 'confirmed';
if($model->update('usergroup')){
$this->redirect( array('page','view'=>'activationed'));
}
} else {
throw new CHttpException(404,'Az aktiválandó regisztráció nem létezik vagy már vissza lett igazolva.');
}
}
The problem is: the code is always go in to the else statement and overwrite the usergroup attribute to ‘confirmed’ and miss the redirect, except if we delete the line where we overwrite the usergroup ("$model->usergroup = ‘confirmed’;"), or we delete the if statement where we update the model:
Hy Bizley, I changed it, but It is not solved the problem.
The code at now:
public function actionActivation($email, $key) {
$model=User::model()->findByAttributes(array('email'=>urldecode($email), 'verificationkey'=>$key, 'usergroup'=>'registered'));
if($model!==null) {
$model->usergroup = 'confirmed';
if($model->update(array('usergroup'))){
$this->redirect( array('page','view'=>'activationed'));
}
} else {
throw new CHttpException(404,'Az aktiválandó regisztráció nem létezik vagy már vissza lett igazolva.');
}
}
Thank you Bizley! We tried to debug with exit statement. After it we solved the problem. We do not understand why does it work, but it is working very well.
Here is the code:
public function actionActivation($email, $key) {
$model=User::model()->findByAttributes(array('email'=>urldecode($email), 'verificationkey'=>$key, 'usergroup'=>'registered'));
echo " ";
if($model!==null) {
$model->usergroup = 'confirmed';
if($model->update(array('usergroup'))){
$this->render('activation');
}
} else {
throw new CHttpException(404,'Az aktiválandó regisztráció nem létezik vagy már vissza lett igazolva.');
}
}
We changed redirect to render (it does not work woth redirect), and the line with echo is important because if we do not use it does not work too. Now it is OK. If anybody know why was that, we are interested in.
Once more thank you Bizley we tried it again because your instructions.
echo shouldn’t be here. I understand you didn’t want to redirect user after successful activation to /page/activationed but instead you wanted the view ‘activation’ to be displayed as it is now.
Keep the view in case something bad during the update happens. You can send variables to your view and display the messages based on it.
public function actionActivation($email, $key) {
$model=User::model()->findByAttributes(array('email'=>urldecode($email), 'verificationkey'=>$key, 'usergroup'=>'registered'));
if($model) {
$model->usergroup = 'confirmed';
if($model->update(array('usergroup'))){
$message = 'Thank you for activation.';
}
else {
$message = 'There was an error while activating your account.';
}
$this->render('activation', array('message' => $message));
}
else {
throw new CHttpException(404,'Az aktiválandó regisztráció nem létezik vagy már vissza lett igazolva.');
}
}
We know the echo is unnecessary, but it need to the code to work on our server. We are glad you to new code it is so elegant we will use flash messages, but on our server it does not work. After we finished the project we will see the server’s settings to find what is wrong with it. Thank you Bizley, u have so much great idea.