简体   繁体   English

如何从数据库中获取Yii中选择列表的键,值对列表?

[英]How do I get a list of key, value pairs from the database for a select list in Yii?

How do I get a list of key/value pairs to populate the options to a relation in a select list in Yii? 如何获取键/值对的列表,以将选项填充到Yii的选择列表中的某个关系中?

Similar to the following in Rails: 与Rails中的以下内容类似:

= f.select :age_group_id, AgeGroup.order(:name).pluck(:name, :id)

Get the list of values, perhaps in the controller 获取值列表,也许在控制器中

$campaigns = Campaign::find()->select('name')->indexBy('id')->column();

Display the list 显示清单

<?= $form->field($model, 'campaign_id')->dropDownList($campaigns) ?>

Could be useful somethings like his 可能对他有用

<?= $form->field($model, 'your_field')->listBox( ArrayHelper::map(Country::find()->all(), 'country_id', 'Country_description'),['prompt'=>'']) ?>

or for dropdown 或下拉菜单

<?= $form->field($model, 'your_field')->dropDownList( ArrayHelper::map(Country::find()->all(), 'country_id', 'Country_description'),['prompt'=>'']) ?>

change country_id, your_field and country_description for your need 根据需要更改country_id,your_field和country_description

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM