How not to write the field "password_repeat" or others fileds in mongoDB

37 views
Skip to first unread message

Augusto Lima

unread,
Oct 24, 2012, 7:04:29 PM10/24/12
to yiimong...@googlegroups.com
Hello everyone, I am new to the group, and I'm a big doubt.

In my registration page, I have the fields:
"password" and "password_repeat"

class Users extends EMongoDocument {
        public $ email;
        .....
        public $ password;
        public $ password_repeat
      
        public function rules () {
       
        return array (
                       array ('email', 'email', 'on' => 'register')
                        array ('email', 'EMongoUniqueValidator')
                       array ('password_repeat', 'compare', 'compareAttribute' => 'password')
             
           ......
                );
        }
}

I want only the field "password" to be saved, but both are being recorded in mongodb.

I used "$model->unsetAttributes (array ('password_repeat'));" but only made clear the value of the field and writes as empty.

I found the only way to solve my problem was:
Validate before and lists the fields that are written by the command $ model-> save (false, array ('.........')).
if ($model-> validate ()) {
        / / $model-> unsetAttributes (array ('password_repeat')); EMPTY
        / / unset ($ model-> password_repeat); ERROR
        if ($ model-> save (false, array ('displayName', 'email,' password ', ..........)))
                $this-> redirect (Yii :: app () -> user-> returnUrl);
}

I want only the field "password" to be saved, but both are being recorded in mongodb.

I used "$ model-> unsetAttributes (array ('password_repeat'));" but only made clear the value of the field and writes as empty.

encotrei the only way to solve my problem was
Validate before and lists the fields that are written by the command $ model-> save (false, array ('.........')).
if ($ model-> validate ()) {
         / / $ model-> unsetAttributes (array ('password_repeat')); EMPTY
/ / unset ($ model-> password_repeat); ERROR
if ($ model-> save (false, array ('displayName', 'email,' password ', ..........)))
                  $ this-> redirect (Yii :: app () -> user-> returnUrl);
}

There is another method of doing this without having to use the "save" this way? just telling the program to remove the fields I do not want to save!


Thanks

Attila Nagy

unread,
Oct 27, 2012, 6:03:57 AM10/27/12
to yiimong...@googlegroups.com
Hello Augusto,

I specially don't like the Yii philosophy of using the same model
class in forms and in data persistence, because it leads to problems
like yours. You could use the scenarios, but either it sometimes does
not work well, or makes your ruleset quite complicated after a while.

So in my practice I usually have a separate model for the form
(extending CFormModel), and the model that I use for persistence.

So I would create one class like:

class Users extends EMongoDocument {
public $email;
public $name;
public $password;
public $lastLoginDate;
// etc
}

then have a registration form like:
class RegistrationForm extends CFromModel {
public $email;
public $name;
public $password;
public $password_repeat;
}

(and maybe a login form like):
class LoginForm extends CFormModel {
public $email;
public $password;
}

In this case each of them had one or just a few scenarios, easily
understandable ruleset, and you shouldn't worry about the changes
resulting in surprising results.

You can use the mass update to move values between models using the
getAttributes/setAttributes methods:
$dbUser->setAttributes($regForm->getAttributes());

// or to avoid warnings about unsafe attributes:
$dbUser->setAttributes(array_diff_key($regForm->getAttributes(),
array('password_repeat' => 1)));


--
Nagy Attila Gabor
Reply all
Reply to author
Forward
0 new messages