How to change the display field of a select using CakePHP3

0

I'm new to CakePHP and am encountering a difficulty to display the data in the right way.

// src/Template/Bookmarks/add.ctp

echo $this->Form->control('user_id', ['options' => $users,'empty' => 'Selecione']);

The above excerpt shows me a select with the user ID, which is my foreign key, but despite wanting to keep the value ( value="" ) of the field as the ID, in the view would like the user's email. Is there any simple way to change this view?

    
asked by anonymous 05.10.2018 / 21:52

1 answer

0

Within the Tables file (in the BookmarksTables.php # src/Model/Table/BookmarksTables.php case), in the initialize() function add setDisplayField() to $this->belongsTo(); . in the end it looks something like this:

$this->belongsTo('Bookmarks', [
    'foreignKey' => 'user_id',
    'joinType' => 'INNER'
])->setDisplayField(
    '<column_name_user>'
);

result:

<select name="user_id" id="user-id">
    <option value="7">Felipe Sá</option>
</select>
    
29.10.2018 / 15:43