Yii Framework Forum: presistent user data with - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

presistent user data with Rate Topic: -----

#1 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 27 November 2008 - 02:01 AM

hey, this is only my second day playing with Yii so I don't know much.  I am considering moving here from CakePHP.

I read the Creating First Yii Application tutorial and from there changed the login system to use my User model instead of the LoginForm model.  Also I moved the login and logout actions to the user controller from the sites controller.  This seemed like the right thing to do. Am I right?  I then changed authenticate() in class UserIdentity to the following:

<?php
public function authenticate() {
$record=User::model()->findByAttributes(array('username'=>$this->username));

if ($record === null) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} elseif ($record->password !== md5($this->password)) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} else {
$this->username = $record->username;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}

Now my problem is I can't figure out how to make the user info persistent. Right now it correctly persists the user name, but I need email and group fields also persisted.  How can I do this?

Also, in my rules I have set these rules:
array('username, password, email, group_id', 'required'),

I chose these rules because on the registration page, all of those fields are required.  But now that I am back to testing the login form, it is broken, because the email field is not required on that form, only on the registration form.  What is the Yii way of handling this?

ps. does Yii have any convenience debug functions? for instance cake had pr($arg) which dumped $arg inside of <pre> tags.  very handy.  Also is there a way to get Yii to tell you a summary of the mysql quarries made?
0

#2 User is offline   wei 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 147
  • Joined: 04-October 08

Posted 27 November 2008 - 05:06 AM

For debugging, try "print_r" or "var_dump" (see php docs).

There is also logging see http://www.yiiframew.../topics.logging

In particular CWebLogRoute has option to enable logging to FireBug (see http://www.yiiframew...#showInFireBug)
0

#3 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 27 November 2008 - 09:32 AM

Don't forget about CVarDumper::dump,
As to your other questions -
Regarding the "Persisting" of other fields, you could provide an getter that retrieves your record (using the "Id") or store them in the CWebUser class you extended.
Regarding the rules, depends on where you changed the rule, if it is on the record you can qualify the rule with an "on" read full details [ur=http://www.yiiframework.com/doc/api/CModel#rulesl]here[/url]

nz
0

#4 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 27 November 2008 - 02:54 PM

@wei I am aware of php's dumping functions.  Although I am new to Yii I am not new to PHP.  The firebug thing looks sexy though. I will check that out.

@notzippy ah CVarDumper::dump is what I was looking for, thanks.

I am storing all the validation rules in the model. This is what it looks like:
<?php
public function rules()
{
return array(
array('username', 'length', 'max'=>50),
array('password', 'length', 'max'=>50),
array('password', 'authenticatePass'),
array('email', 'length', 'max'=>100),
array('email_confirmed', 'length', 'max'=>21),
//array('email', 'required', array('on'=>'insert')),
array('username, password, group_id', 'required'),
array('group_id', 'numerical', 'integerOnly'=>true),
);
}
?>


I read about the on=>insert setting but when I uncomment the line above Yii gets an error in the core. I think I'm doing something wrong.

But the 'on'=>insert is not a satisfactory solution to me.  What if I have two forms that insert a entry, but one of them needs a field while the other doesn't?  Is there another way?

For instance in my cakephp setup I could do the following:


<?php
if ($UserModel->validates('a list of fields that are required')) {
//validates
}

So I could hard-code the required fields in the action of the controller.  If the field was not required it would ignore all of the field's validation rules set in the model.  Made more sense that way. eg.


<?php

//registration action
if ($UserModel->validates('password, password_confirm, email, username')) {
//validates
}


//login action
if ($UserModel->validates('password, username')) {
//validates
}


//change password action
if ($UserModel->validates('old_password, new_password, new_password_confirm')) {
//validates
}


Hopefully from the above example you can see how much more useful this method is.  Is there anything similar in Yii?
0

#5 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 27 November 2008 - 03:02 PM

Hmm

could you do

loginModel extends CFormModel  {
  public function rules()
  {
      return array(
        array('username', 'length', 'max'=>50),
        array('password', 'length', 'max'=>50),
        array('password', 'authenticatePass'),
      );
  }
}

public signupModel extends loginModel {
  public function rules()
  {
      return array_merge(parent::rules(),array(
        array('email', 'length', 'max'=>100),
        array('email_confirmed', 'length', 'max'=>21),
        //array('email', 'required', array('on'=>'insert')),
        array('username, password, group_id', 'required'),
        array('group_id', 'numerical', 'integerOnly'=>true),
      ));
  }
}


then validate against the correct model depending on the form ?

nz
0

#6 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,905
  • Joined: 04-October 08
  • Location:DC, USA

Posted 27 November 2008 - 03:16 PM

I just changed the validate() method to allow specifying specific attributes to be validated. If you do not specify any attributes, all attributes listed in rules() will be validated.
0

#7 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 27 November 2008 - 04:32 PM

@qiang you just changed that in the core? thanks! that's awesome.


@notzippy maybe it is different in Yii but I have never heard of any framework that that would be recommended.  Generally, a model represents a object, eg. a table in your mysql database.  Not an action in a controller, that seems ridiculous to me.  I could outline many reasons.
0

#8 User is offline   notzippy 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 06-October 08

Posted 27 November 2008 - 04:38 PM

If you look at my code I specified a CFormModel this is a model based on data in a HTML form rather then a table. That is the reason I suggested it.
0

#9 User is offline   jonah 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 733
  • Joined: 27-November 08
  • Location:California (US)

Posted 27 November 2008 - 07:34 PM

Still, aren't the cFormModels simply just models without a database table attached? for instance, you would use a cFormModel for a feedback page, if the feedback page did not have a table but simply email the quarry to the site admin, for an example.  That still seems ridiculous to me.
0

#10 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,905
  • Joined: 04-October 08
  • Location:DC, USA

Posted 27 November 2008 - 08:38 PM

Quote

@qiang you just changed that in the core? thanks! that's awesome.

Yes.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users