This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi,
All of my database tables, even the backend_users one, have two columns:
- created_by_id (owner)
- edited_by_id (editor)
Both columns have a relationship with the id column of the backend_users table. All my models have the relationships set properly. It simply works. This way I know which user created or edited a content. I've extended backend User model in my plugin boot method in order to set the relationship between the columns id and created_by_id and edited_by_id but it does not work. It's like I can't load a relationship that references a column of the same table. I can query $model->owner where $model is a model different from backend User and get the correct owner model; I can't do the same when $model is an instance of backend User. It's like I can't query a user from a user. Very strange to me. Is there a solution or it's a bug?
My boot method:
use Backend\Models\User as BackendUser;
// [...]
public function boot()
{
BackendUser::extend(function ($model) {
$model->belongsTo = array_merge($model->belongsTo ?? [], [
'owner' => [
BackendUser::class,
'key' => 'created_by_id',
'otherKey' => 'id'
],
'editor' => [
BackendUser::class,
'key' => 'edited_by_id',
'otherKey' => 'id'
]
]);
});
}
I think the problem here is that your Backend User model may belong to MANY Backend User records... so you may need a belongsToMany relation and a pivot table for the relation.
1-3 of 3