CRUD View: _getIndexTitleField() must be of type string, array returned

Setup

You're using CakePHP with CRUD plugin installed. You baked some models from the database.

Problem

You visit the index page for a given model and you get the following error:

CrudView\Listener\ViewListener::_getIndexTitleField(): Return value must be of type string, array returned

Solution

Make sure the model has a display field. You can always try using the ID, if the table has it:

$this->setDisplayField('id');

This happens on tables that are join tables with additional columns. In other words, tables like articles_tags that may have some columns beyond the expected article_id and tag_id. In such cases, bake reasonably decides to make a dedicated model class for them, but when there are no display field candidates (such as name or title), it will use a composite display field out of those two foreign IDs. And CRUD View does not support that.

Tip

You can use a virtual field as a display field.

In your Table class:

    $this->setDisplayField('title');

In the corresponding Entity class:

protected array $_accessible = [
    'title' => true,
    // ...
];

protected function _getTitle()
{
    return 'Example'; // You can use associated data here, if it's available
}