This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

info13179
info13179

Hello,

I want to allow multiple authors (store owners) to post blogposts. On every blogpost I want to show a description of the author. So, I was thinking to extend the backend user model with a description field. What is the best way to do this?

I now have plugin with this code:

Plugin.php

``namespace Roywcm\BloeinestBlog;

use System\Classes\PluginBase; use Event; use Backend\Controllers\Users as UsersController; use Backend\Models\User as UserModel;

class Plugin extends PluginBase { public function registerComponents() { }

public function registerSettings()
{
}

public function boot(){

    UserModel::extend(function ($model) {
        $model->hasOne['storeinfo'] = ['Roywcm\BloeinestBlog\Models\Storeinfo'];
    });

    Event::listen('backend.form.extendFields', function($widget) {

        // Only for the User controller
        if (!$widget->getController() instanceof UsersController) {
            return;
        }

        // Only for the User model
        if (!$widget->model instanceof UserModel) {
            return;
        }

        // Add an extra description field
        $widget->addFields([
            'storeinfo[description]' => [
                'label'   => 'Beschrijving van winkel/ondernemer',
                'type'    => 'textarea'
            ]
        ]);
    });

}

} ``

model

``<?php namespace Roywcm\BloeinestBlog\Models;

use Model;

class Storeinfo extends Model { use \October\Rain\Database\Traits\Validation;

public $rules = [
];

public $timestamps = false;

protected $fillable = ['description'];

public $belongsTo = [
    "id" => ["Backend\Models\User"]
];

public $table = 'roywcm_bloeinestblog_storeinfo';

}``

But now, i'm getting this error: "Call to a member function hasRelation() on null" on line 56 of /path/to/modules/backend/Traits/FormModelSaver.php"

Last updated

chris10207
chris10207

you need to make sure that your model has a relation instance to the storeinfo first

you can achieve that with something like this in your plugin boot method

if (!$model->storeinfo) {
     $storeinfo = new StoreInfo;
      $model->storeinfo = $storeinfo;
      $model->save();
 }

// code not tested

Last updated

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.