class User extends CActiveRecord { public function beforeSave(){ if($this->isNewRecord) { // generate a new usercode if the record is new $this->usercode = uniqid(); } parent::beforeSave(); } }In the above example, we have overridden the beforeSave() method of CActiveRecord class, which has checks if the record that is being saved is a new record, it generates a new unique user code for the user record. The parent method is then called to complete the save process. Package Library: Gii - A code generation framework for Yii.