{"id":1631,"date":"2024-07-27T09:50:19","date_gmt":"2024-07-27T08:50:19","guid":{"rendered":"https:\/\/editjournal.redakt.eu\/faxmodem\/?p=1631"},"modified":"2024-07-27T09:51:02","modified_gmt":"2024-07-27T08:51:02","slug":"crud-view-getindextitlefield-type-string-array","status":"publish","type":"post","link":"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/quick-bites\/crud-view-getindextitlefield-type-string-array\/","title":{"rendered":"CRUD View: _getIndexTitleField() must be of type string, array returned"},"content":{"rendered":"<h1>Setup<\/h1>\n<p>You're using CakePHP with CRUD plugin installed. You baked some models from the database.<\/p>\n<h1>Problem<\/h1>\n<p>You visit the index page for a given model and you get the following error:<\/p>\n<blockquote>\n<p>CrudView\\Listener\\ViewListener::_getIndexTitleField(): Return value must be of type string, array returned<\/p>\n<\/blockquote>\n<h1>Solution<\/h1>\n<p>Make sure the model has a display field. You can always try using the ID, if the table has it:<\/p>\n<pre><code>$this-&gt;setDisplayField(&#039;id&#039;);<\/code><\/pre>\n<p>This happens on tables that are join tables with additional columns. In other words, tables like <code>articles_tags<\/code> that may have some columns beyond the expected <code>article_id<\/code> and <code>tag_id<\/code>. In such cases, bake reasonably decides to make a dedicated model class for them, but when there are no display field candidates (such as <code>name<\/code> or <code>title<\/code>), it will use a composite display field out of those two foreign IDs. And CRUD View does not support that.<\/p>\n<h1>Tip<\/h1>\n<p>You can use a virtual field as a display field.<\/p>\n<p>In your Table class:<\/p>\n<pre><code>    $this->setDisplayField('title');<\/code><\/pre>\n<p>In the corresponding Entity class:<\/p>\n<pre><code>protected array $_accessible = [\n    'title' => true,\n    \/\/ ...\n];\n\nprotected function _getTitle()\n{\n    return 'Example'; \/\/ You can use associated data here, if it's available\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Setup You&#8217;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&hellip; <a class=\"more-link\" href=\"https:\/\/editjournal.redakt.eu\/faxmodem\/blog\/development\/cakephp\/quick-bites\/crud-view-getindextitlefield-type-string-array\/\">Continue reading <span class=\"screen-reader-text\">CRUD View: _getIndexTitleField() must be of type string, array returned<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1234],"tags":[],"class_list":["post-1631","post","type-post","status-publish","format-standard","hentry","category-quick-bites","entry"],"_links":{"self":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/comments?post=1631"}],"version-history":[{"count":1,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1631\/revisions"}],"predecessor-version":[{"id":1633,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/posts\/1631\/revisions\/1633"}],"wp:attachment":[{"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/media?parent=1631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/categories?post=1631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/editjournal.redakt.eu\/faxmodem\/wp-json\/wp\/v2\/tags?post=1631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}