[PLUGIN.PHP]
<?php namespace Alex\Clients;
use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use Rainlab\User\Controllers\Users as UsersController;
use Alex\Clients\Models\Clients as ClientsModel;
/**
* Clients Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Clients',
'description' => 'No description provided yet...',
'author' => 'Alex',
'icon' => 'icon-leaf'
];
}
public function boot()
{
UserModel::extend(function($model)
{
$model->hasOne['clients'] = ['Alex\Clients\Models\Clients'];
});
UsersController::extendFormFields(function($form, $model, $context){
if(!$model instanceof UserModel)
return;
if (!$model->exists)
return;
ClientsModel::GetFromUser($model);
$form->addTabFields([
'clients[clients]' => [
'label' => 'Client',
'tab' => 'Client'
],
]);
});
}
}
[CLENTS.PHP]
<?php namespace Alex\Clients\Models;
use Model;
/**
* Clients Model
*/
class Clients extends Model
{
/**
* @var string The database table used by the model.
*/
public $table = 'alex_clients_clients';
/**
* @var array Guarded fields
*/
protected $guarded = ['*'];
/**
* @var array Fillable fields
*/
protected $fillable = [];
/**
* @var array Relations
*/
public $belongsTo = [
'user' => ['RainLab\User\Models\User']
];
public static function getFromUser($user)
{
if ($user->clients)
return $user->clients;
$clients = new static;
$clients->user = $user;
$clients->save();
$user->clients = $clients;
return $clients;
}
}
[FIELD.YAML]
# ===================================
# Form Field Definitions
# ===================================
fields:
id:
label: ID
disabled: true
clients:
label: clients
[COLUMNS.YAML]
# ===================================
# List Column Definitions
# ===================================
columns:
id:
label: ID
searchable: true
clients:
label: clients
searchable: true