Yii Framework Forum: Proposal: AR attribute modification state - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Proposal: AR attribute modification state Rate Topic: -----

#1 User is offline   sdietz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 12
  • Joined: 04-July 09

Posted 08 September 2009 - 07:05 PM

Hi Yii-Community.

I'm currently working on a small private project using the trunk version of Yii.

Tonight I found it quite useful if the ActiveRecord would provide some sort of modification state per attribute beside the already existing new state of a record instance.

To illustrate this further:
Assume an already existing User decides to change his user data.
Further the users password is encrypted by a class specific encryption method.

Now, after submitting the form, the attributes are massively assigned and the save()-method is called.
To ensure the password is encrypted properly the encryption method is called in the beforeSave method.

If the user has changed his login password everything is fine. If he only changed his email-address,
the password would be encrypted again, rendering the user unable to log into the application again.

Conclusion and actual proposal:
If, during the massive assignment, the User object could set a modification state to all changed
attributes the encryption method could later check if the password attribute was changed and act accordingly (encrypt the new password od leave the unchanged password alone).

Thinking further, AR could decide to only update actually changed values, maybe improving performance.

What would you think about the usefulness of such feature?

Hoping for a nice discussion,

Steffen
0

#2 User is offline   qiang 

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

Posted 08 September 2009 - 07:14 PM

The fact is when the form is submitted, the password attribute will always be changed, even if the user doesn't enter anything.

For this password problem, you may refer to the following code in beforeSave()
if($this->isNewRecord)
{
    $this->salt=md5(mt_rand());
    $this->password=$this->generatePasswordHash($this->password,$this->salt);
}
else
{
    if(!empty($this->password))
        $this->password=$this->generatePasswordHash($this->password,$this->salt);
    else
        $this->password=self::model()->findByPk($this->id)->password;
}

0

#3 User is offline   pestaa 

  • past Yii dev member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 705
  • Joined: 07-May 09
  • Location:Hungary

Posted 09 September 2009 - 03:27 AM

I usually extend the model's setter, so I don't have to worry about rehashing passwords.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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