Howto get associated fields as options in a form and not the id field only?

Hi there,

This might be a very stupid question from a newbie on CakePHP 4 (I did some work on CakePHP 2 years ago and this was never a problem as far as I remember), but although I consulted the tutorial carefully, I don’t get it done!

I have a table “Earnings” with the following fields:
id
date
customer_id
description
amount

and a corresponding “Customer” table where I store my clients
id
fullname

Now, when I add a new earning, I want to be able to select the customer by its fullname and not by its id. However, the scaffolded “add” function only provides me the customer_id in the form and not the fullname.

The template line for the form looks like this:
echo $this->Form->control('customer_id', ['options' => $customers]);

How can I change the options to get the fullname field instead of the id field in the dropdown list?

Thanks for your help!
Marcus.

In CustomersTable::initialize, use $this->displayField('fullname');

1 Like