DropDownList yii 2.0 example

pvaitonis picture pvaitonis · Oct 27, 2014 · Viewed 44k times · Source

I am using yii 2.0 Framework. How i can make options from my database. I found this, but it's yii 1.1:

<?php echo CHtml::dropDownList('listname', $select, 
          array('M' => 'Male', 'F' => 'Female'));

I want to pass it to form:

<?php $form->dropDownList() ?>

How i can fill my dropdownlist from my database table?

Answer

olchick picture olchick · Nov 4, 2014

If you use ActiveForm widget use this:

<?php 
    $items = ArrayHelper::map(Model::find()->all(), 'id', 'name');
    $form->field($model, 'attribute')->dropDownList($items)
?>