M
M
Michael2018-12-13 21:32:02
Yii
Michael, 2018-12-13 21:32:02

How to display the name of the field corresponding to this id in the form instead of the number (id)?

Hello everybody! Please tell me, when switching to the view by one of the values, I pass the identifier of the parent element. After switching to the view, the identifier of the parent element (parent_id) is set in the corresponding field, but since it is not very convenient to display the identifier, I would like to see instead of id - the name of the field (name) corresponding to the given id. In this case, I have id = 1, and the field name = User's workplace. Tell me how to display its name instead of an identifier?
5c12a580ef6d4483698266.png
At the moment, I have written such a structure,

$parent = Type::find()->where(['parent_id' => 0])->all();
$items = ArrayHelper::map($parent, 'id', 'name');

The printout looks like this
Array
(
    [1] => Рабочее место пользователя
    [5] => Периферийные устройства
    [11] => СПД/ЛВС
    [28] => Билетно-кассовое оборудование
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abdula Magomedov, 2018-12-13
@Bally

$form->field($model, "field")->dropDownList($items, ['prompt' => 'Выберите'])

You can immediately get an indexed array from the base.
$items = Type::find()->select(['name'])->where(['parent_id' => 0])->indexBy('id')->column();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question