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

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   raveboy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 20-June 13

Posted 21 August 2013 - 02:30 PM

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
0

#2 User is offline   Alexandre Rodichevski 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 15-August 13
  • Location:Italy

Posted 22 August 2013 - 01:24 PM

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

#3 User is offline   Shekeel 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 22-August 13
  • Location:Canada

Posted 23 August 2013 - 02:35 AM

View PostAlexandre Rodichevski, on 22 August 2013 - 01:24 PM, said:

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.yiiframew...istration-form/
0

#4 User is offline   raveboy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 20-June 13

Posted 23 August 2013 - 07:37 AM

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
0

#5 User is offline   Alexandre Rodichevski 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 15-August 13
  • Location:Italy

Posted 23 August 2013 - 11:31 AM

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
0

#6 User is offline   raveboy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 20-June 13

Posted 28 August 2013 - 12:50 PM

View PostAlexandre Rodichevski, on 23 August 2013 - 11:31 AM, said:

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
0

#7 User is offline   Rodrigo 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 29-May 10

Posted 29 August 2013 - 11:09 AM

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.yiiframew...cord#beforeSave
http://www.yiiframew...ewRecord-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....ntroller.php#51


Regards,
Rodrigo
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users