0

我有一个与用户(用户)表相关的配置文件(配置文件)表,

现在,在前端我想更新用户信息,我也在 octobercms 更新用户个人资料信息

怎么做?

先感谢您

Profile.php(配置文件模型)

**//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(配置文件)

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'
            ],
             .....
        ]);

    });
}

形式

我使用默认帐户组件(Rainlab 用户),并添加一些字段(配置文件字段)

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

4

1 回答 1

1

这不起作用,因为rainlab.user.afterUpdate在rainlab 用户插件中没有类似的事件。这是所有rainlab 用户插件事件的列表。

现在,如果没有事件,你将如何做到这一点?

因此,如果您仅在更新时触发此类事件。您可以在表单中再添加一个隐藏字段,就像您在更新表单中添加额外字段的问题中提到的那样。

所以只需添加这个字段

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

在这一步之后,您必须在您尝试访问事件的Plugin.phprainlab.user.afterUpdate文件中进行更改。

只需添加条件

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

因此,ProfileModel::getFromUser($user);仅当您尝试更新它时才会触发。

只需替换此代码

$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'
        ],
         .....
    ]);
});

我还没有测试过,但我认为这会奏效。如有任何疑问,请发表评论。

于 2018-05-07T13:20:21.403 回答