Create dropdownlist from column in DB

Dear all

I have been struggling with this simple problem for some time now. I have a table in my DB which has a column named Year. What I would like to do is to have a view (website) which has a simple dropdown list containing the distinct years in.

Making a selection in the dropdown would reload the page with that year as the parameter.

So my questions are:


[*] How do you make an ActiveForm without a model?

[*] How do I can I get the distinct years from the column (it contains double entries)?

[*] How do I get the dropdown to appear in the view with the corresponding actions?


Kind regards

Hi eothein,


[*] You can’t do that, because an ActiveFrom requires a model.

But don’t be disappointed, because what you want to do can be achieved much easier with it than without it.

Just start with the Gii generated CRUD codes for your target model.

[*] Create some static method that retrieves the distinct years in your model.

[*] Replace the text field for ‘Year’ in ‘_search’ form of the index page with a dropdown.


You may need some sample code. Please tell us more about your model that you want to show in your page.

Hi first thanks for you reply!

What I basically want is get all the distinct years from my column, show them in a dropdown and reload the page after another year (from those years from that column) is selected (the page show relevant information for that year).

I can create a model for it, but I found it too cumbersome and to much boiler plate code for such an easy functionality?

Yes, you should create a model for your db table using Gii’s model generator, and also you should create CRUD pages for that model. It’s the easiest way.

Then my last question is how do I get the models with a distinct on the column. What I have now is

$jaren = ArrayHelper::map(app\models\reservatie\Prijszetting::find()->all(),'prijszetting_id','prijszetting_jaar');

but that gives me all the records in the table.

Kind regards

Is ‘prijszetting_id’ the PK of the table?

You don’t need it to filter the result.

Just use a dropdown containing distinct 'prijszetting_jaar’s in place of the text filed for ‘prijszetting_jaar’ in ‘_search’ form.

And you could use ‘distinct’.

... Prijszetting::find()->select('prijszetting_jaar')->distinct()->all()->asArray() ...

I used another approach after searching for some time.

For getting the associative array with distinct values for prijszetting_jaar I used

$jarenModels = app\models\reservatie\Prijszetting::find()

        ->select(['prijszetting_id', 'prijszetting_jaar'])->groupBy('prijszetting_jaar')


$jaren = ArrayHelper::map($jarenModels, 'prijszetting_id', 'prijszetting_jaar');

To create a more enchanced dropdown I used Kartik Select2 widget:

<div class="col-sm-1 col-sm-offset-11">


            $formJaar = ActiveForm::begin(['type' => ActiveForm::TYPE_HORIZONTAL]);

            echo Form::widget([

                'formName' => 'jaarForm',

                'form' => $formJaar,

                'columns' => 1,

                'attributes' => [

                    'jaar' => [

                        'type' => Form::INPUT_WIDGET,

                        'widgetClass' => '\kartik\widgets\Select2',

                        'label' => 'Jaar',

                        'options' => [

                            'data' => $jaren,