save() returning false, but why?

Hi, just starting out with Yii and while trying to set up user registration have hit the following problem:

Here is my actionRegister method in SiteController.php




        public function actionRegister()

        {

            $model=new User;


            if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')

            {

                    echo CActiveForm::validate($model);

                    Yii::app()->end();

            }


            if(isset($_POST['User']))

            {

                $model->attributes=$_POST['User'];




                    // Check if question has a ? at the end

                    $last = $model->question[strlen($model->question)-1];

                    if($last !== "?")

                    {

                            $model->question .= '?';

                    }

                        // validate user input and redirect to previous page if valid

                        if($model->validate())

                        {

                                // save user registration


                                $result = $model->save(false);

                                var_dump($result);


                                if (!$result){

                                    var_dump($model->attributes);

                                    var_dump($model->getErrors());

                                    die();

                                }


                                

                                

                        }

                }

                // display the registration form

                $this->render('register',array('model'=>$model));

                    

        }

}



And here is my User model class:


<?php


class User extends CActiveRecord

{


         public $password2;

         public $verifyCode;


	/**

	 * Returns the static model of the specified AR class.

	 * @return user 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 '{{user}}';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

            return array(

                        array('username','length','max'=>32),

                        // convert username to lower case

                        array('username', 'filter', 'filter'=>'strtolower'),

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

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

                        // compare password to repeated password

                        array('password', 'compare', 'compareAttribute'=>'password2'),

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

                        // make sure email is a valid email

                        array('email','email'),

                        // make sure username and email are unique

                        array('username, email', 'unique'),

                        array('question','length','max'=>256),

                        // convert question to lower case

                        array('question, answer', 'filter', 'filter'=>'strtolower'),

                        array('answer','length','max'=>128),

                        //Profile Length

                        array('profile','length','max'=>500),

                        // convert answer to lower case

                        array('username, password, password2, email, question, answer, verifyCode', 'required'),

                        // verifyCode needs to be entered correctly

                        array('verifyCode', 'captcha', 'allowEmpty'=>!extension_loaded('gd')),

                );




	}


	/**

	 * @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',

			'salt' => 'Salt',

			'email' => 'Email',

			'profile' => 'Profile',

                        'question' => 'Secret Question',

                        'answer' => 'Secret Answer',

		);

	}


	/**

	 * 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('username',$this->username,true);


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


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


		return new CActiveDataProvider('user', array(

			'criteria'=>$criteria,

		));

	}


        public function validatePassword($password)

        {

                return User::hashPassword($password,$this->salt)===$this->password;

        }


        public static function hashPassword($password,$salt)

        {

                return md5($salt.$password);

        }


        public function beforeSave()

        {

                $this->salt = md5(mt_rand());

                $this->password = User::hashPassword($this->password, $this->salt);

        }




}

My problem is that upon submitting the registration form save() comes back as false and I dont have a clue why.

Here is the debug message I get with a test registration:


boolean false


array

  'username' => string 'bvncncn' (length=7)

  'password' => string 'd9fe3f2bc9cbafd13caca67fd0f041d8' (length=32)

  'email' => string 'fdfhd@fdg.com' (length=13)

  'profile' => string 'DHFGLSBNFG,KLSBG' (length=16)

  'question' => string 'fdghfh?' (length=7)

  'answer' => string 'hfhdfhdfhg' (length=10)

  'salt' => string '83553cb1b9108c5656ef86d3ad7c9484' (length=32)

  'id' => null


array

  empty



The first result is a var_dump of the result of $model->save(false)

The second is the array $model->attributes

The third result is the array $model->getErrors()

Any help would be great!

Thanks

The beforeSave() method must return true, you should return the result of a call to the parent impementation. BTW the signature should be "protected function beforeSave()".

/Tommy

(not a team member)

Thanks Tommy!