Find Query on a dropdownlist

I have a dropdown employee list in tickets form. In my employee model there’s a status ‘Available’ and ‘Not Available’

If I change the status of employee to ‘Not Available’ it won’t appear on the dropdown list. I have one employee that has set to ‘Not Available’ status but it still appears on the dropdown list.

Here’s what I did:




<?= $form->field($model, 'employee_id')->dropDownlist(

                                                ArrayHelper::map(Employee::find([

                                                    'status' => 'Available'])->all(), 'id', 'employee_name'),

                                                [

                                                    'prompt' => 'Select Employee ID',

                                                    'style' => 'width:200px'

                                                ]); ?>



I got it now by doing




    <?= $form->field($model, 'employee_id')->dropDownlist(

                           ArrayHelper::map(Employee::find()->where(['status' => 'Available'])->all(), 'id', 'employee_name'),

                           [

                            'prompt' => 'Select Employee ID',

                            'style' => 'width:200px'

                          ]); ?>



I’m not sure if this is the right way. But it works fine.

it is reasonably well and it works, although you could improve this by creating a scope or custom query

http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#customizing-query-classes