RecordFinder in a jsonable attribute throws error

I am working on a plugin, where a model(“Race”) has a repeater (“race_control”, text => jsonable) which contains a recordfinder to select a RainLab User Model.

This works fine if I set useRelation to false and set modelClass to “RainLab\User\Models\User”.

But in the template I now only have the id of the related model. It would be much easier for me if I could get the related Model directly, but turning useRelation to true results in an error stating

"Model 'AramLoosman\Races\Models\Race' does not contain a definition for 'user_item'." on line 551 of /var/www/html/web/modules/backend/classes/Controller.php

This is a snippet of my “Race”-Model’s fields yaml:

race_control:
            label: 'Race control'
            type: repeater
            tab: 'Race control'
            form:
                fields:
                    user_role:
                        label: 'Role'
                        type: text
                    user_item:
                        label: User
                        type: recordfinder
                        list: ~/plugins/rainlab/user/models/user/columns.yaml
                        recordsPerPage: 10
                        title: Find User
                        prompt: Click the Find button to find a user
                        nameFrom: name
                        descriptionFrom: email
                        useRelation: true
                        modelClass: RainLab\User\Models\User

Is there a way for me to keep the relation in a repeater?

Take a look at this model, instead of using jsonable (the model itself can use it instead): test-plugin/RepeaterItem.php at master · octobercms/test-plugin · GitHub

Then

public $hasMany = [
    'sisters' => [
        RepeaterItem::class,
        'key' => 'parent_id',
        'delete' => true
    ]
]

Then

sisters:
    tab: Sister Countries
    type: repeater

With table

Schema::create('october_test_repeater_items', function($table) {
    $table->increments('id');
    $table->integer('parent_id')->unsigned()->nullable()->index();
    $table->mediumText('value')->nullable();
    $table->timestamps();
});

Thanks for your message.
If I understand it correctly, you suggest using an intermediary table instead of jsonable?

That’s what I thought to do, but then I tried using the jsonable because I’ve used it with tailor and it worked such a treat :slightly_smiling_face:

Just for the record (and anyone in the future looking for smth. like this), this is how I made it work with jsonable:

I’ve added a custom property to my model “Race”:
public $preloaded_race_control = [];

And populated/unset it using two events:

    public function beforeSave() {
        unset($this->preloaded_race_control);
    }

    public function afterFetch() {
        $items = [];
        foreach( $this->race_control as $item) {
            $user = User::find($item['user_item']);
            $item['user_item'] = $user;
            $items[] = $item;
        }
        $this->preloaded_race_control = $items;
    }

This is a nice solution too!

The one I gave is how Tailor does it internally.