Rainlab builder truncate text in pivot data

I build my backend with rainlab builder plugin

I’ve found that with that plugin you can also input pivot data in many relationships as mentioned here in the docs.

As you can see in the docs, it can select from pivot data, but what I want is also to truncate the text from pivot data

So I make it like this

teams:
    label: Team
    view:
        list:
            columns:
                name:
                    label: Name
                pivot[team_color]:
                    label: Team color
                    select: 'IF(CHAR_LENGTH(team_color) > 100, CONCAT(LEFT(team_color, 100),"..."), team_color)'
    manage:
        list:
            columns:
                name:
                    label: Name
    pivot:
        form:
            fields:
                pivot[team_color]:
                    label: Team color

But what I get is this error

Call to a member function getTable() on null
~/modules/backend/widgets/Lists.php line 503

How do I solve this ?
or are there any other ways to achieve this result ?

Hi @wibisono.indrawan

It looks like the model may not be configured correctly, can you share the model class with the relationship definitions?

Hi, thanks for replying

Here is my actual code

config_relation for model subject

levels:
  label: Levels
  view: 
      list:
        columns:
          name:
            label: Name
          pivot[description_top_html]:
            label: Description Top
          pivot[description_bottom_html]:
            label: Description Bottom
      toolbarButtons: add|remove
  manage:
    list: $/indra/myproject/models/level/columns.yaml
    form: $/indra/myproject/models/level/fields.yaml
    showCheckboxes: true
    showSearch: true
    showSorting: true
    defaultSort: level_id
    recordsPerPage : 20
  pivot:
    form:
        fields:
            pivot[description_top_html]:
                label: Description Top
                type: markdown
                size: giant
            pivot[description_bottom_html]:
                label: Description Bottom
                type: markdown
                size: giant

relation in model subject

    public $belongsToMany = [
        'levels' => [
            'indra\myproject\models\Level',
            'table' => 'indra_myproject_subjects_levels',
            'pivot' => ['description_top_html', 'description_bottom_html'],
        ]
    ];

relation in model level

public $belongsToMany = [
        'subjects' => [
            'indra\myproject\models\Subject',
            'table' => 'indra_myproject_subjects_levels',
            'pivot' => ['description_top_html', 'description_bottom_html'],
        ]
    ];

Everything looks fine. The error occurs because of this line:

$this->model->makeRelation($column->relation)->getTable();

Effectively the model cannot make the relation (probably pivot). We will need to try to reproduce it using the Test plugin.

It’s possible that select is not compatible with the pivot relationship. So a workaround could be to remove the select and replace it with a partial type and truncate the text using PHP instead. Here is a link to the partial list column:

1 Like

Yep, I’ve also just done it with a partial view as well and it works perfectly

I’ll share my workaround here just in case for others if they need it

config_relation.yaml

evels:
  label: Levels
  view: 
      list:
        columns:
          name:
            label: Name
          pivot[description_top_html]:
            label: Description Top
            type: partial
            path: ~/plugins/indra/myproject/controllers/subjects/_column_truncate_text.htm
          pivot[description_bottom_html]:
            label: Description Bottom
            type: partial
            path: ~/plugins/indra/myproject/controllers/subjects/_column_truncate_text.htm
      toolbarButtons: add|remove
  manage:
    list: $/indra/myproject/models/level/columns.yaml
    form: $/indra/myproject/models/level/fields.yaml
    showCheckboxes: true
    showSearch: true
    showSorting: true
    defaultSort: level_id
    recordsPerPage : 20
  pivot:
    form:
        fields:
            pivot[description_top_html]:
                label: Description Top
                type: markdown
                size: giant
            pivot[description_bottom_html]:
                label: Description Bottom
                type: markdown
                size: giant

the partial view

<?= strlen($value) > 100 ? substr($value,0,100)."..." : $value ?>

NB :
By the way, the select statement that I posted in my question works if it’s not pivot data

1 Like