sql to fill a dropdown input

I want to execute a query to fill a dropdown list,I do the bellow code but it is not what I want,how can I take the query elements the way I want?


 $type = (new \yii\db\Query())

    ->select(['id', 'name'])

    ->from('buildtype')    

    ->All();

the view


<?= $form->field($model, 'type')->dropdownList($type); ?>

[edit] Now i have this code


foreach ($rows as $key => $value) {

    $type[$value['id']]=$value['name'];

}

Maybe you want:




<?= $form->field($model, 'type')->dropdownList( \yii\helpers\ArrayHelper($type, 'id', 'name') ); ?>




<?= $form->field($model, 'type')->dropdownList( \yii\helpers\ArrayHelper($type, 'id', 'name') ); ?>

This code gets the error Call to undefined function yii\helpers\ArrayHelper()

How can I use that?

do not need to query again.

you can use directly like this




use yii\helpers\ArrayHelper;


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

    ArrayHelper::map(app\models\Type::findAll(), 'id', 'name'),

   ['prompt' => 'Select Type']



use relation (foreign key) in the database.