0

我想通过创建自己的名为Users的插件在后端管理员中添加更多字段。到目前为止,这是我为创建几个新领域所做的工作。

插件\technobrave\users\Plugin.php

<?php namespace Technobrave\Users;

use System\Classes\PluginBase;
use Backend\Models\User as BackendUserModel;
use Backend\Controllers\Users as BackendUsersController;
class Plugin extends PluginBase
{
    public function registerComponents()
    {
    }

    public function registerSettings()
    {
    }

    public function boot()
    {
          // Add college and teacher field to Users table
        BackendUserModel::extend(function($model){
            $model->belongsTo['team'] = ['technobrave\team\Models\Team'];            

        });




         // Add college and teacher field to Users form
         BackendUsersController::extendFormFields(function($form, $model, $context){

            if (!$model instanceof BackendUserModel)
                return;

            $form->addTabFields([
                'team' => [
                    'label'   => 'Team',
                    'comment' => 'Associate this user with a team.',
                    'type' => 'recordfinder',
                    'list' => '$/technobrave/team/models/team/columns.yaml',
                    'prompt' => 'Click the %s to find a team',
                    'select' => 'id',
                    'nameFrom'=> 'name',
                    'tab' => 'Account'
                ]
            ]);
        });
    }
}

插件\technobrave\users\models\User.php

<?php namespace Technobrave\Users\Models;

use Model;


/**
 * Model
 */
class User extends Model
{



    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
        'team_id' => 'required',

    ];

    public $customMessages = [
                'team_id.required' => 'Please select Team',




    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'backend_users';
}

现在这就是我的管理员页面的样子。

在此处输入图像描述

如果我点击“Team Recordfinder”,我可以看到如下所示。

在此处输入图像描述

如果我从列表中选择任何团队记录,它如下所示。

在此处输入图像描述

如您所见,到目前为止一切正常。但是,一旦我填写完整的表格,我就会收到这个 SQL 错误,说找不到列。如下所示。

“SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列 'team_id'(SQL:插入backend_users( is_superuser, login, email, first_name, last_name, password, persist_code, permissions, team_id, updated_at, created_at) 值 (0, Johny, johny@mailinator.com, Johny, Cook, y$bbr00J5O2dE1WDHHWZYHIeMduXI82HkDnE8IYBcAet4ie0nfpgpwq, , , 9, 2017-05-19 06:23:49, 2017-05-19 06:23:49)"在 C:\xampp\hdsites\slp_web 的第 666 行\laravel\framework\src\Illuminate\Database\Connection.php

在此处输入图像描述

现在,我的问题是,我需要在backend_usersteam_id中手动添加字段吗?这是有效的方式吗?我不知道。

此外,我想对这个特定字段进行验证,因此我在下面做了。

插件\technobrave\users\models\User.php

<?php namespace Technobrave\Users\Models;

use Model;


/**
 * Model
 */
class User extends Model
{



    use \October\Rain\Database\Traits\Validation;

    /*
     * Validation
     */
    public $rules = [
        'team_id' => 'required',

    ];

    public $customMessages = [
                'team_id.required' => 'Please select Team',




    ];

    /**
     * @var string The database table used by the model.
     */
    public $table = 'backend_users';
}

但是验证也不起作用。如果有人从用户角色中检查了“物业顾问”,我该如何验证此字段?

很多问题,但我需要一个最好的方法来解决这个问题。有人可以指导我使这件事起作用吗?

谢谢

4

1 回答 1

1

我需要在 backend_users 表中手动添加 team_id 字段吗?这是有效的方式吗?我不知道。

是的,您需要创建迁移以将此字段添加到表中。

要添加验证规则,您还可以扩展模型:

public function boot() { 
    BackendUserModel::extend(function($model){
         $myrules = $model->rules;
         $myrules['team_id'] = 'required';
         $model->rules = $myrules;
    });
}

要获得登录用户,您可以使用 App::before

public function boot() { 
    \App::before(function() { 
        // Here BackendAuth::getUser() is already initialized 
    )};
}
于 2017-05-19T07:58:56.587 回答