Yii-User: Create New User And Then Trigger An Event

Hello there,

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?

Thanks in advance

Jan

Hi raveboy.

I think you can modify the extension. Add or reload to the afterSave() method of the user model.

@ Alexandre Rodichevski

please also provide me some guideline to resolve my problem,

I am also using yii-user Extension,

and My problem is here:

http://www.yiiframework.com/forum/index.php/topic/46415-yii-user-registration-form/

Hello Alex,

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) {

		(...)

	}

}



Please be more specific… thank you

Jan

Hi raveboy.

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.

Best wishes

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 }”…?

Thanks Jan

User->afterSave() is called every time the model is updated (i.e. on it’s creation, on login - lastvist field, on update it self).

In User->beforeSave() you can use if ($this->getIsNewRecord()) - this will be true only on User creation

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#beforeSave

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#getIsNewRecord-detail

Or you can put your code on <app>\protected\modules\user\controller\RegistrationController.php

RegistrationController->actionRegistration() inside the if ($model->save()) {} after the $profile->save(); line.

https://code.google.com/p/yii-user/source/browse/trunk/modules/user/controllers/RegistrationController.php#51

Regards,

Rodrigo