Find and Form Helper - show more information

Hi I have a question about the finder and Form->control.
I have a Post which contains clients. So far so good.
In my add method, I put in there a list of clients

$clients = $this->Tickets->Clients->find('list', limit: 200)->all();
$this->set(compact( 'clients'));

On my template, I have a form where I have a option select where I can choose clients.

echo $this->Form->control('client_id');

But when I have a look on the Posts/add site, then I see the option list just only the name of the client. But I want to display in this field, name and prename.
Is there a possibility to put these additional info to the form control?
Thanks

Maybe this is helpful:
https://book.cakephp.org/5/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs

Hi dirk, thanks for your fast answer. I think I will try this find(fields) what is described in your link.
But on template, there is nothing to change?

The view uses whatever is in $clients as an array of key-value pairs. The list finder gives you an array of key-value pairs, where the key is the ID and the value is the display field (typically name, for example). If you want the value to be different, you have to make that happen in your controller. Retrieving Data & Results Sets - 5.x shows an example of how to do that.

I tried it like described on the page
find(ā€˜listā€™, keyField: ā€˜idā€™, valueField: ā€˜author.nameā€™)
But it is same as before, I have an ID and a name which is shown. But I need 2 Fields displayed.

You need to use something like the example in the ā€œCustomize Key-Value Outputā€ section, which I linked directly to. Something along the lines of

$clients = $this->Tickets->Clients->find('list',
        keyField: 'id',
        valueField: function ($client) {
            return $client->name . ' ' . $client->prename;
        }
    );
1 Like

:slight_smile: you are faster,
I solved it in meanwhile with your hint from the link above and created this.

->find('list', [
        'valueField' => function ($row) {
            return $row['first_name'] . ' ' . $row['last_name'];
        }
    ])