Is there any way to access the old attribute values after using the setAttributes method to assign new values?
I need to update a field if only the users has input some new values. in the other hand if the field is empty, save the old value (which is set as empty by not entering any values by the user).
I wrote a basic history logging behavior which does this:
private $oldAttributes;
public function afterFind($event)
{
$this->oldAttributes = $this->getOwner()->getAttributes();
}
If you made $oldAttributes public or wrote an accessor for it, this could work for you. After creating the behavior, you’d just include it in the models which need the functionality like this:
public function behaviors()
{
return array(
'ActiveRecordHistoryLogger'
=> 'application.models.behaviors.ActiveRecordHistoryLogger',
);
}
I know that this is a bit of an older post, but I wanted to offer a simpler solution that uses less memory.
The Yii AR models already store an array with the old values, so there is no need to clone them again. Simply access them like this:
$model->oldValues['attributeName']
For example in your User model you may want to make sure the password is hashed when updated.
public function beforeSave()
{
if ($this->password != $this->oldValues['password']) { // password has changed, so we need to hash it.
$this->password = CPasswordHelper::hashPassword($this->password,13);
}
return parent::beforeSave();
}