I want to add few more fields in my backend administrator by creating my own plugin called as Users. Here is what I have done so far to create few new fields.
plugins\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'
]
]);
});
}
}
plugins\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';
}
And now this is how my Administrator page looks like.
And if I click on "Team Recordfinder" I am able to see like below.
And if I select any of the team record from the list, its looks like below.
As you can see all working fine so far. But as soon as I fill full form, I am getting this SQL error saying column not found. Like below.
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_id' in 'field list' (SQL: insert into
backend_users(is_superuser,login,first_name,last_name,password,persist_code,permissions,team_id,updated_at,created_at) values (0, Johny, [email protected], Johny, Cook, y$bbr00J5O2dE1WDHHWZYHIeMduXI82HkDnE8IYBcAet4ie0nfpgpwq, , , 9, 2017-05-19 06:23:49, 2017-05-19 06:23:49))" on line 666 of C:\xampp\htdocs\slp_website_cms\vendor\laravel\framework\src\Illuminate\Database\Connection.php
Now, My question is, do I need to add team_id field manually in backend_users table ? Is it valid way ? I am not sure.
Additionally, I want to make validations for this particular field hence I did below.
plugins\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';
}
But validations are not working either. How can I validate this field if some one checked "Property Consultant" from User Role ?
Lot of questions but I need a best way to approach towards this issue. Can someone guide me to make this thing work ?
Thanks
Github issues really isn't the place for questions like this. Please use one of the many other avenues available instead of submitting new issues for your questions. That being said, I'll take a quick look at this for you
Forum: http://octobercms.com/forum
StackOverflow: http://stackoverflow.com/questions/tagged/octobercms
IRC: #october on Freenode or http://octobercms.com/chat
Slack: https://octobercms-slack.herokuapp.com/
You will need to add team_id to the backend_users table for what you're currently trying to do to work. Make sure if you go that route that you remove that column in your down() method of your migration.
An alternative is using a manyToMany relationship instead between teams and backend users (and artificially limiting it to users having only one team) which wouldn't require any changes to the backend_users table.
Ok. Thanks for the instructions.
Most helpful comment
You will need to add
team_idto the backend_users table for what you're currently trying to do to work. Make sure if you go that route that you remove that column in yourdown()method of your migration.An alternative is using a manyToMany relationship instead between teams and backend users (and artificially limiting it to users having only one team) which wouldn't require any changes to the backend_users table.