简体   繁体   中英

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?

Similar to the following in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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