Ares Draguna
Ares Draguna

Reputation: 1661

Yii2 dropdownlist WITHOUT $model

I have searched the web far and wide for a solution to this problem. I already know the Yii2 dropdown way is this:

<?php
use yii\helpers\ArrayHelper;
use backend\models\Standard;
?>

<?= Html::activeDropDownList($model, 's_id',
      ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>

But I want to make the dropdown without the $model... Is there ANY way to do this?

Thank you in advance!

Upvotes: 10

Views: 37391

Answers (2)

Insane Skull
Insane Skull

Reputation: 9358

You can also use this:

public function getAll()
{
    $get = Standard::find()->all();
    $result = ArrayHelper::map($get, 'id', 'name');
    return $result;
}

Then dropdown:

<?= Html::dropDownList(Standard::getAll(), ['prompt' => '--- select ---']) ?>

This will solve your error.

Upvotes: 5

Barry
Barry

Reputation: 3723

You can also use

Html::dropDownList()

<?= Html::dropDownList('s_id', null,
      ArrayHelper::map(Standard::find()->all(), 's_id', 'name')) ?>

See Yii Manual

Upvotes: 30

Related Questions