I’m currently using Yii-User extension and I’d like to trigger an event (run the additional function) while saving a newly created user (by clicking “Create” after filling the form). Where do I find the code for that?
there’s no afterSave() method either in the User model from Yii or from the Yii-User extension (there’s also no afterSave anywhere else in the code of my app). Does it mean i should add it to the source code and that’s it?
The code of \myapp\protected\models\User.php is only:
class User extends CActiveRecord
{
public static function model($className=__CLASS__)
{
return parent::model($className);
}
public function tableName() {
return 'tbl_yii_users';
}
}
and in \myapp\protected\modules\user\models\User.php there are only these functions:
class User extends CActiveRecord
{
(...)
/**
* Returns the static model of the specified AR class.
* @return CActiveRecord the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return Yii::app()->getModule('user')->tableUsers;
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
(...)
}
/**
* @return array relational rules.
*/
public function relations()
{
(...)
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
(...)
}
public function scopes()
{
(...)
}
public function defaultScope()
{
return array(
'select' => 'id, username, email, createtime, lastvisit, superuser, status',
);
}
public static function itemAlias($type,$code=NULL) {
(...)
}
}
I mean the extension’s class, i.e. \myapp\protected\modules\user\models\User.php, because it is exactly the code you want to customize. The afterSave() method is implicitly included in this class because it is inherited from the parent CActiveRecord class. In order to customize this method add it explicitly in any place of the User.php:
public function afterSave()
{
// your custom code
// ...
return parent::afterSave();
}
Do not forget to call at the end the parent’s afterSave() method.
Hello, it works OK, but I realised the application calls the function by user login as well. Seems like the User model is used for both logging in a user and saving a new user. How can I ask whether the form I’m in is only the one for saving? Something like “if($this_is_a_save_user_form) { // my custom code }”…?