[英]Extends User plugin by adding a profile does not render tab either new added fields in OctoberCMS
I've follow all the steps on the Extending User plugin screencast but for some reason I can not see "Profile" tab and either new added fields. 我已按照扩展用户插件截屏视频的所有步骤操作,但出于某种原因,我看不到“配置文件”选项卡和新添加的字段。 Since I used the second approach, the easy one, this is what I've done:
既然我使用了第二种方法,那就是简单方法,这就是我所做的:
Create and make the needed changes to the files as explained in video: 按照视频中的说明创建并对文件进行必要的更改:
Plugin.php <?php namespace Alomicuba\\Profile; use System\\Classes\\PluginBase; use RainLab\\User\\Models\\User as UserModel; use RainLab\\User\\Controllers\\Users as UsersController; /** * Profile Plugin Information File */ class Plugin extends PluginBase { public $requires = ['RainLab.User']; /** * Returns information about this plugin. * * @return array */ public function pluginDetails() { return [ 'name' => 'Profile', 'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User', 'author' => 'DTS', 'icon' => 'icon-users' ]; } public function boot() { UserModel::extend(function($model){ $model->hasOne['profile'] = ['Alomicuba\\Profile\\Models\\Profile']; }); UsersController::extendFormFields(function ($form, $model, $context){ if ($model instanceof UserModel) return; $form->addTabFields([ 'pinCode' => [ 'label' => 'PIN', 'tab' => 'Profile' ], 'phone2' => [ 'label' => 'Teléfono (2)', 'tab' => 'Profile' ], 'phone3' => [ 'label' => 'Teléfono (3)', 'tab' => 'Profile' ], 'phone4' => [ 'label' => 'Teléfono (4)', 'tab' => 'Profile' ] ]); }); } } add_profiles_fields_to_user_table.php <?php namespace Alomicuba\\Profile\\Updates; use Schema; use October\\Rain\\Database\\Updates\\Migration; class AddProfilesFieldsToUserTable extends Migration { public function up() { Schema::table('users', function($table) { $table->integer('pinCode')->unsigned(); $table->dateTime('pinCodeDateTime'); $table->integer('phone2')->unsigned()->nullable(); $table->integer('phone3')->unsigned()->nullable(); $table->integer('phone4')->unsigned()->nullable(); }); } public function down() { $table->dropDown([ 'pinCode', 'pinCodeDateTime', 'phone2', 'phone3', 'phone4' ]); } } version.yaml 1.0.1: First version of Profile 1.0.2: - Created profiles table - create_profiles_table.php - add_profiles_fields_to_user_table.php Profile.php (Model) <?php namespace Alomicuba\\Profile\\Models; use Model; /** * Profile Model */ class Profile extends Model { /** * @var string The database table used by the model. */ public $table = 'alomicuba_profile_profiles'; /** * @var array Relations */ public $belongsTo = [ 'user' => ['RainLab\\User\\Models\\User'] ]; // This method is not need anymore since I'll use the second approach public static function getFromUser($user) { if ($user->profile) return $user->profile; $profile = new static; $profile->user = $user; $profile->save(); $user->profile = $profile; return $profile; } }
But when I edit a existent user I didn't see the 'Profile' tab and also didn't see any new added field. 但是,当我编辑现有用户时,我没有看到“配置文件”选项卡,也没有看到任何新添加的字段。 See image below:
见下图:
Any advice around this? 对此有何建议? Did I miss something?
我错过了什么?
Also I have a few question around plugin extends: 我还有一些关于插件扩展的问题:
I haved tested your code on my machine you need to write 我已经在你需要写的机器上测试了你的代码
$require
instead of $requires
in plugin.php $require
而不是plugin.php中的$requires
please check documentation 请检查文档
http://octobercms.com/docs/plugin/registration#dependency-definitions http://octobercms.com/docs/plugin/registration#dependency-definitions
and when extendFormFields() method called for UserController you need to specify that you only want to extends fields for UserModel not for other 并且当为UserController调用extendFormFields()方法时,您需要指定您只想为UserModel扩展字段而不是为其他
if (!$model instanceof UserModel)
return;
so plugin.php code look like this 所以plugin.php代码看起来像这样
<?php namespace Alomicuba\Profile;
use System\Classes\PluginBase;
use RainLab\User\Models\User as UserModel;
use RainLab\User\Controllers\Users as UsersController;
/**
* Profile Plugin Information File
*/
class Plugin extends PluginBase
{
public $require = ['RainLab.User'];
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'Profile',
'description' => 'Add extra functionalities for Alomicuba WS by extends RainLab User',
'author' => 'DTS',
'icon' => 'icon-users'
];
}
public function boot()
{
UserModel::extend(function($model){
$model->hasOne['profile'] = ['Alomicuba\Profile\Models\Profile'];
});
UsersController::extendFormFields(function ($form, $model, $context){
if (!$model instanceof UserModel)
return;
$form->addTabFields([
'pinCode' => [
'label' => 'PIN',
'tab' => 'Profile'
],
'phone2' => [
'label' => 'Teléfono (2)',
'tab' => 'Profile'
],
'phone3' => [
'label' => 'Teléfono (3)',
'tab' => 'Profile'
],
'phone4' => [
'label' => 'Teléfono (4)',
'tab' => 'Profile'
]
]);
});
}
}
and in add_profiles_fields_to_user_table.php 在add_profiles_fields_to_user_table.php中
for dropping column write following code 用于删除列写下面的代码
Schema::table('users', function($table)
{
$table->dropDown([
'pinCode',
'pinCodeDateTime',
'phone2',
'phone3',
'phone4'
]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.