从另一个 table 用于 radioList 的 select 选项

Using from another table for radioList's select options

我想为 radioList 的 select 选项使用 另一个 table 数据。

为了变得更好我的意思是,我想要类似下面的代码:

            $form->field($model_add, 'link_type')->radioList([

               'another table ID 1' => 'another table title 1',
               'another table ID 2' => 'another table title 2',
            ]);

我可以在其中使用 foreach 作为选项吗?

如果我的问题不清楚,请问我你想知道更多。

您可以为此使用 ArrayHelper。假设 $array 看起来像这样:

[
    ['id' => 1, 'title' => 'aaa'],
    ['id' => 2, 'title' => 'bbb'],
    // ...
]

你可以映射到

[
    1 => 'aaa',
    2 => 'bbb',
    // ...
]

像这样:

$form->field($model_add, 'link_type')->radioList(
    \yii\helpers\ArrayHelper::map($array, 'id', 'title')
);

和Bizley的回答一样,有点不清楚

<?= $form->field($model, 'fieldName')->radioList( 
        ArrayHelper::map(TableName::find()->all(), 'id', 'name'),
        ['prompt' => 'Please Select']);?>