This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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
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