I Can Read Records From Database, But Cant Update Records

I have a registration form that generates a activation link to be emailed once a user registers. Once the user clicks the link, its suppose to update the status of user to active, but it is not. Everything is working up to that point.

Here is the code of interest.


 public function actionActivation ()

        {

		$email = $_GET['email'];

		$activationkey = $_GET['activationkey'];

		if ($email&&$activationkey) {

			$find = Users::model()->findByAttributes(array('email'=>$email));

			if (isset($find)&&$find->status=="active") {

			    $this->render('/site/message',array('title'=>"User activation",'content'=>"You account is active."));

			} elseif(isset($find->activationkey) && ($find->activationkey==$activationkey)) {

                                //PROBLEM RIGHT HERE!!NOT UPDATING RECORD!

				$find->activationkey = md5(microtime());

				$find->status='active';

				$find->save();

			    $this->render('/site/message',array('title'=>"User activation",'content'=>"You account is activated."));

			} else {

			    $this->render('/site/message',array('title'=>"User activation",'content'=>"Incorrect activation URL."));

			}

		} else {

			$this->render('/site/message',array('title'=>"User activation",'content'=>"Incorrect activation URL."));

		}

	}

Here is the model


<?php


/**

 * This is the model class for table "users".

 *

 * The followings are the available columns in table 'users':

 * @property string $id

 * @property string $username

 * @property string $password

 * @property string $email

 * @property string $registered

 * @property string $activationkey

 * @property string $status

 * @property string $url

 */

class Users extends CActiveRecord

{

        public $verifyPassword;

        public $verifyEmail;

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Users the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'users';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('username, password, email, url', 'required'),

			array('username, activationkey, status, url', 'length', 'max'=>64,'min'=>3),

                        array('password', 'length', 'max'=>64, 'min'=>6),

                        array('status', 'default', 'value' => 'not active', 'setOnEmpty' => true, 'on' => 'insert'),

			array('email', 'length', 'max'=>100),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, username, password, email, registered, activationkey, status, url', 'safe', 'on'=>'search'),

                        array('email', 'length', 'max'=>100),

                        array('email','email'),

                        array('username', 'unique', 'message' =>'Username already exsist'),

                        array('url', 'unique', 'message' =>'Url already taken'),

                        array('email', 'unique', 'message' => 'Email already exsist'),

                        array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u','message' =>'Incorrect symbols (A-z0-9).'),

                        array('registered', 'default', 'value' => date('Y-m-d H:i:s'), 'setOnEmpty' => true, 'on' => 'insert'),

                        array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' =>'Password does not match.'),

                        array('verifyEmail', 'compare', 'compareAttribute'=>'email', 'message' =>'Email does not match.'),

			array('id, username, password, email, registered, activationkey, status, url', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'username' => 'Username',

			'password' => 'Password',

			'email' => 'Email',

			'registered' => 'Registered',

			'activationkey' => 'Activationkey',

			'status' => 'Status',

			'url' => 'Url',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id,true);

		$criteria->compare('username',$this->username,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('registered',$this->registered,true);

		$criteria->compare('activationkey',$this->activationkey,true);

		$criteria->compare('status',$this->status,true);

		$criteria->compare('url',$this->url,true);


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

Are you sure it reaches the $find->save() ? If so var_dump($find->getErrors()); This will get you some more info on why the model isn’t saving.

Im sure. I alter $find->status=‘active’; and it throws a cant find error. I added your suggestion and this is what i get


array(2) {

  ["verifyPassword"]=>

  array(1) {

    [0]=>

    string(24) "Password does not match."

  }

  ["verifyEmail"]=>

  array(1) {

    [0]=>

    string(21) "Email does not match."

  }

}

Ok it seems like the rules for password and email validation are being triggered, these are being called because the scenario of the model you’re saving is probably set to update.

I recommend changing the scenario of your model in your actionActivation() function. Then in your rules you specify that the attributes(verifyPassword & verifyEmail) are not needed. This way the validation will stay for your insert and update scenario’s but not for your ‘activation’ scenario.

Take a look here for more info on scenarios

Works like a charm! Thanks a lot! I learned something new.