How to exchange a select display field using Cakephp3

Asked

Viewed 67 times

-1

I’m new to Cakephp and am finding it difficult to display data correctly.

// src/Template/Bookmarks/add.ctp

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

The above section shows me a select with the user ID, which is my foreign key, however, although I want to keep the value (value="") field as the ID, in the view would like the user’s email. Is there any simple way to change this view?

1 answer

0


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

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

outworking:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.