So I’m writing my application administration interface. I used yiic tool to create my model and my crud files.
On my model ‘Users’ i invoked beforeValidate and beforeSave methods to change the attributes.
public function beforeValidate()
{
if(parent::beforeValidate())
{
$this->setAttribute('birthday', date('Y-m-d', strtotime($this->bYear . '-' . $this->bMonth . '-' . $this->bDay)));
$this->setAttribute('activationKey', Yii::app()->user->_encrypt(microtime() . $this->password));
$this->setAttribute('dateCreation', date('Y-m-d H:i:s'));
$this->setAttribute('dateLastVisit', date('Y-m-d H:i:s'));
return true;
}
}
public function beforeSave()
{
if(parent::beforeSave())
{
$this->setAttribute('firstName', strtoupper($this->firstName));
$this->setAttribute('lastName', strtoupper($this->lastName));
$this->setAttribute('birthday', date('Y-m-d', strtotime($this->bYear . '-' . $this->bMonth . '-' . $this->bDay)));
$this->setAttribute('password', Yii::app()->user->_encrypt($this->password));
$this->setAttribute('activationKey', Yii::app()->user->_encrypt(microtime().$this->password));
$this->setAttribute('secretAnswer', Yii::app()->user->_encrypt(strtolower($this->secretAnswer)));
$this->setAttribute('status', 1);
}
return true;
}
Question 1
The problem is that I don’t want this same attributes values on different scenarios ‘create’ and ‘update’.
How can I use this code only when I’m creating a new user?
How can I use another code when I’m updating an existing user?
Question 2
Another newbie question is how can I save the ‘dateLastVisit’ when the user login? I was trying to use the below code on my UserIdentity.php
$model=new Users;
$model->dateLastVisit=date('Y-m-d H:i:s');
$model->save();
Question 3
If you see the fisrt code above you’ll note that i encrypted the password
$this->setAttribute(‘password’, Yii::app()->user->_encrypt($this->password));
but when the admin edit and save the user the password get encrypted again. How can I solve this?