Update Attributes Php Version 5.2.6

Hy members,

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:


			if($model->update('usergroup')){

				$this->redirect( array('page','view'=>'activationed'));

			}

If we have both in the code it doesn’t work but if we has only one of them it is working, but we need both to change the attribute in the database.

We tried to setAttribute function and we set the usergroup rule to safe.

The code is working very well on localhost with new PHP version. But we cant update the PHP on the server.

Finally we want to overwrite the usergroup attribute to ‘confirmed’.

Sorry for my english it is not the best c(:

We ask for help! Thank you very much to read it, have a nice Day!

Method update() takes one parameter and it is the array of attributes that need to be saved or null. Change this and try again.

Hy Bizley, I changed it, but It is not solved the problem. :confused:

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 for reply.

Have you tried to debug this by adding exit statements? Are the databases for your local and remote servers the same?

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.');

    }

}



Usually we use flash messages for this. See this wiki http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/

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.