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() }}