Doesn't update

Here is model:




class Users extends CActiveRecord {

    

    public $password_repeat;

    

    public function rules() {

        return array(

            array('username, firstname', 'required'),            

            array('username', 'length', 'min'=>3),

            array('lastname', 'length', 'min'=>1),

            array('username', 'unique', 'on'=>'create'),

            array('username', 'match', 'pattern'=>'/^[a-z_0-9\.\-]+$/is'),

            array('email', 'email'),

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

            // passwords must required on create user

            array('password, password_repeat', 'required', 'on'=>'create'),

            // password syntax

            array('password', 'match', 'pattern'=>'/^[a-z_0-9\.\-]+$/is'),

            // compare passwords

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

        );

    }    

    

    public static function model($className=__CLASS__)

    {

        return parent::model($className);

    }

 

    public function tableName()

    {

        return '{{users}}';

    }

}



And I am trying to update:




$model = Users::model()->findByPk(34);  

        if ($model) {

            $model->setAttributes(array(

                'username' => 'joker',

                'password' => md5('password')

            ));

            $model->save();

        }



What am I doing wrong ?

Get $model->save() return value. If this is false, check errors in $model->errors.

Try this:




$model = Users::model()->findByPk(34);  

        if ($model) {

            $model->setAttributes(array(

                'username' => 'joker',

                'password' => md5('password')

            ));


            if($model->save() == false)

            {

                  print_r($model->errors);

            }

        }



Probably you are missing some validation rules.

hi

likely you have errors in rules.

if you want don`t call function ruls() , use save(false)

You can’t save beacause you forgot to set the password_repeat field! This rule prevents you from saving:


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

You should do this:


$model->setAttributes(array(

      'username' => 'joker',

      'password' => md5('password'),

      'password_repeat' => md5('password'), 

));