Save user profile details, October CMS

1k Views Asked by At

I have a profile(profile) table in relation with the user(user) table,

now, at the frontend I would like to update the user information that I also update the user profile information, on octobercms

how to do?

thank you in advance

Profile.php (profile Model)

**//Relation with User**
public $belongsTo = [
    'user' => ['RainLab\User\Models\User'],
];

public static function getFromUser($user){
        if ($user->profile)
            return $user->profile;

        $profile = new static;
        $profile->user = $user;
        $profile->save();

        return $profile;
}

Plugin.php (Profile)

public function boot()
    {
        UserModel::extend(function ($model) {
            $model->hasOne['profile'] = ['GeniusIdea\Profile\Models\Profile'];

            $model->bindEvent('model.afterSave', function() use ($model) {
                 **//it works well when registering the user at the front end**
                \Event::listen('rainlab.user.register', function($user) {
                    ProfileModel::getFromUser($user);
                });

                 **//It does not work,**
                \Event::listen('rainlab.user.afterUpdate', function($user) {
                    ProfileModel::getFromUser($user);
                });
            });
        });
**//it works well when updating user information in the backend**
    UserController::extendFormFields(function ($form, $model, $context) {

        if (!$model instanceof UserModel)
            return;


        if (!$model->exists)
            return;


        ProfileModel::getFromUser($model);

        $form->addTabFields([
            'profile[skills]' => [
                'label' => 'Skills',
                'tab' => 'Professional',
                'type' => 'text'
            ],
            'profile[experience]' => [
                'label' => 'Years of experience',
                'tab' => 'Professional',
                'type' => 'number'
            ],
            'profile[address]' => [
                'label' => 'Professional address',
                'tab' => 'Professional',
                'type' => 'text'
            ],
             .....
        ]);

    });
}

Form

I use a default account component(Rainlab User), and add some fields (profile fields)

{{ form_ajax('onUpdate', { model: user }) }}
    <div class="row">
       <div class="four columns">
            <label for="accountName">Nom</label>
          <input name="name" type="text" class="form-control"id="accountName" value="{{ form_value('name') }}">
       </div>
    <div class="four columns">
      <label for="accountName">Prenom</label>
       <input name="surname" type="text" class="form-control" id="accountSurname" value="{{ form_value('surname') }}">
    </div>

                  ............

     <button type="submit" class="btn btn-default">Save</button>

{{ form_close() }}

1

There are 1 best solutions below

1
On

This is not working because there is no event like rainlab.user.afterUpdate in rainlab user plugin. Here is the list of all rainlab user plugin events.

Now how will you do that if there is no event?

So if you fire such event only when updating it. You can add one more hidden field inside your form like you have mentioned in the question that you have added extra fields in update form.

So just add this field

<input type="hidden" name="triggerOnUpdate" value="1">

After this step, you have to do change in your Plugin.php file where you are trying to access the event rainlab.user.afterUpdate.

Just add condition

if(!empty(post('triggerOnUpdate'))){
    ProfileModel::getFromUser($user);
}

So this will fire ProfileModel::getFromUser($user); only when you try to update it.

Just replace this code

$model->bindEvent('model.afterSave', function() use ($model) {
             **//it works well when registering the user at the front end**
            \Event::listen('rainlab.user.register', function($user) {
                ProfileModel::getFromUser($user);
            });

             if(!empty(post('triggerOnUpdate'))){
                 ProfileModel::getFromUser($user);
             }
        });

  UserController::extendFormFields(function ($form, $model, $context) {

    if (!$model instanceof UserModel)
        return;


    if (!$model->exists)
        return;


    ProfileModel::getFromUser($model);

    $form->addTabFields([
        'profile[skills]' => [
            'label' => 'Skills',
            'tab' => 'Professional',
            'type' => 'text'
        ],
        'profile[experience]' => [
            'label' => 'Years of experience',
            'tab' => 'Professional',
            'type' => 'number'
        ],
        'profile[address]' => [
            'label' => 'Professional address',
            'tab' => 'Professional',
            'type' => 'text'
        ],
         .....
    ]);
});

I haven't tested this yet, But I think this will work. Please comment if any doubts.