Proposal: AR attribute modification state

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

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;

}



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