如何实现Rainlab User Plus+ Plugin的Location功能?

How to implement the Location function of User Plus+ Plugin from Rainlab?

我已经通过位置插件在我的前端实现了国家 Select 表单和州 Select 表单。这些值根据 Location 的后端设置进行填充。但是如果我点击注册,国家和州不会被保存。

首先我认为这是一个可填写的问题,因为没有抛出任何错误。然后我查看了代码,意识到 Location Plugin 的文档指出行为控制器向已实现的模型(州和国家/地区)添加了两个新关系。查看位置插件的代码示例,我注意到 select 表单字段被命名为 'country_id' 和 'state_id'。我已将它们更改为国家和州,以便用户模型选择由位置行为创建的关系,但仍然没有运气。字段未保存。我做错了什么?

部分site/country-state-horizontal.htm

 {% set countryId = countryId|default(form_value('country')) %}
 {% set stateId = stateId|default(form_value('state')) %}
 <div class="uk-margin">
     <div class="uk-margin">
         <label class="uk-form-label" for="accountCountry">{{ 'Land'|_      }}</label>
         <div class="uk-form-controls">
             {{ form_select_country('country', countryId, {
             id: 'accountCountry',
             class: 'uk-select',
             emptyOption: '',
             'data-request': 'onInit',
             'data-request-update': {
             'site/country-state-horizontal': '#partialCountryState'
             }
             }) }}
         </div>
     </div>
 </div>

 <div class="uk-margin">
     <label class="uk-form-label" for="accountState">{{ 'Bundesland'|_      }}</label>
     <div class="uk-form-controls">
         {{ form_select_state('state', countryId, stateId, {
         id: 'accountState',
         class: 'uk-select',
         emptyOption: ''
         }) }}
     </div>
 </div>

编辑:

部分snippets/intro.htm

    <form 
     data-request="onRegister"
     class="uk-form-horizontal uk-margin-large">
          <div class="uk-margin">
               <label class="uk-form-label" for="registerName">Vorname</label>
               <div class="uk-form-controls">
                    <input
                            name="name"
                            type="text"
                            class="uk-input"
                            id="registerName"
                            placeholder="Bitte deinen Vornamen eingeben" />
               </div>
          </div>

           <div class="uk-margin">
                <label class="uk-form-label" for="registerSurname">Nachname</label>
                <div class="uk-form-controls">
                   <input name="surname" type="text" class="uk-input" id="registerSurame" placeholder="Bitte deinen Nachnamen eingeben" />
                </div>
           </div>

 .....

           <div id="partialCountryState">
                {% partial 'site/country-state-horizontal' countryId=user.country_id stateId=user.state_id %}
           </div>

 .....

           <div class="uk-margin">
                <button type="submit" class="uk-button uk-button-primary">Registrieren</button>
           </div>

预期:表单在注册国家和州时为用户保存 ID 实际:字段留空

我阅读了插件并发现它 general plugin 所以要使其 兼容 users plugin 我们需要 添加一些额外的那里的代码.

  1. Add needed fields in table ( manually )

我们需要手动添加 country_idstate_id 到 table users

您可以通过从 builder plugin and apply that. make sure it stayscountry_id 和 state_id[ in your case you tried to change them if that so just revert back and add postfix_id`]

创建新版本文件来添加它们
<?php namespace HardikSatasiya\SoTest\Updates;

use Schema;
use October\Rain\Database\Updates\Migration;

class Migration1013 extends Migration
{
    public function up()
    {
        Schema::table('users', function($table)
        {
            $table->integer('country_id')->unsigned()->nullable()->index();
            $table->integer('state_id')->unsigned()->nullable()->index();
        });
    }

    public function down()
    {
        // Remove fields
    }
}
  1. Now in your plugin's boot method add this code
<?php namespace HardikSatasiya\SoTest;

// ... 
use RainLab\User\Models\User as UserModel;

class Plugin extends PluginBase
{

    public function boot() {

      UserModel::extend(function ($model) {
        $model->implement[] = 'RainLab.Location.Behaviors.LocationModel';
        $model->addFillable([
            'country_id',
            'state_id',
        ]);
      });

    // ...

现在尝试保存您的数据,它应该保存 country and state 并检索它们,您可以使用

echo $userModel->country_code; // US
echo $userModel->state_code; // FL

echo $userModel->country_id; // 10 respective id
echo $userModel->state_id; // 22 respective id

安装了 User Plus(+) 插件

Not sure with User Plus(+) plugin but I debugged and find some issue, because of this commit https://github.com/rainlab/location-plugin/commit/6360a319f1e1e3eff8b6a43e85bdfdf3f84fbb58 fillable is not working properly.

所以,我们可以尝试再次手动添加它。

<?php namespace HardikSatasiya\SoTest;

// ... 
use RainLab\User\Models\User as UserModel;

class Plugin extends PluginBase
{

    public function boot() {    
      UserModel::extend(function ($model) {            
        $model->addFillable([
            'country_id',
            'state_id',
        ]);
      });

如有疑问请评论。