Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 929
Ares Draguna Avatar asked Nov 04 '14 12:11

Ares Draguna


2 Answers

You can also use

Html::dropDownList()

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

See Yii Manual

like image 184
Barry Avatar answered Nov 08 '22 15:11

Barry


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.

like image 31
Insane Skull Avatar answered Nov 08 '22 15:11

Insane Skull