【问题标题】:Add slug to user (RainLab) OctoberCms将 slug 添加到用户 (RainLab) OctoberCms
【发布时间】:2018-10-18 14:48:12
【问题描述】:

是否可以在用户表中添加一个slug, 如果可以,那怎么办?

我尝试照常做,但它不适用于表用户

class User extends Model{
use \October\Rain\Database\Traits\Sluggable;

protected $slugs = ['slug' => 'name'];

}

我在用户表中添加 slug 字段, 但它仍然为空

提前谢谢你

【问题讨论】:

    标签: octobercms octobercms-plugins octobercms-backend october-form-controller october-partial


    【解决方案1】:

    会有点长,但没问题,我们会解决的:)

    有两种方法。

    1. 将直接字段添加到user 表并开始使用它,我们就完成了。
    2. 在数据库中创建其他表UserExtension [使用新插件很明显] 并将动态关系oneToOne 添加到用户表到此表,然后使用关系将所有新数据保存到此表。

    现在我们应该选择1st,好像你只想要一个field 所以

    1. 在表[数据库表]中添加实际字段
    2. 扩展backend forms以显示该字段[保存数据将自动工作]

    创建更新脚本 [ 类名 => AddSlugToUserTable ] 文件名 将是蛇形案例 => add_slug_to_user_table.php。将此文件添加到插件的 updates 目录中。

    <?php namespace HardikSatasiya\Plugin\Updates;
    
    use Schema;
    use October\Rain\Database\Updates\Migration;
    
    class AddSlugToUserTable extends Migration
    {
        public function up()
        {
            Schema::table('users', function($table)
            {
                // make it nullable as we are adding it and existing records may not have any data for it so
                $table->string('slug')->nullable()->index();
            });
    
           // or even you can add converted data to slug field
           // from existing user name
        }
    
        public function down()
        {
            // don't want to mess with data so better be empty
        }
    }
    

    现在在version.yaml 文件中添加有关此文件的详细信息。如果它不在updates 文件夹中,则创建此文件。 [ 此文件对间距非常敏感,因此请为制表符使用 2 个空格并避免多余的空格。 ]

    1.0.1:
        - Initialize plugin.
    1.0.2:
        - Adding Slug Field to User Table.
        - add_slug_to_user_table.php
    

    下一步是将表单字段添加到Backend Form,将此代码添加到您的plugin.php =&gt; boot 方法中。

    class Plugin extends PluginBase
    {
        [...]
    
        public function boot()
        {
            // Extend all backend form usage
            \Event::listen('backend.form.extendFields', function($widget) {
    
                // Only for the User controller
                if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
                    return;
                }
    
                // Only for the User model
                if (!$widget->model instanceof \RainLab\User\Models\User) {
                    return;
                }
    
                // Add an extra birthday field
                $widget->addFields([
                    'slug' => [
                        'label'   => 'Slug',
                        'comment' => 'Add Slug To User',
                        'preset'    => [
                             'field' => 'name',
                             'type' => 'slug'
                         ]
                    ]
                ]);
    
            });
        }
    }
    

    现在Logout [如果你已经登录] 从后端和Login 再次生效所有这些东西。然后打开User From.

    您将看到闪亮的新 slug 字段,该字段可以从 name 自动填充 [您可以从预设配置中更改它]

    如果您有任何问题或疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 2017-04-05
      • 2019-08-24
      • 2019-07-02
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2018-06-17
      • 2017-09-14
      相关资源
      最近更新 更多