Display all if is null

In my Gridview I have setup one of the Filters to be based on another filter (dependent select). This works.

[
    'attribute'=>'ExpenseSubCategoryId',
    'value'=>'expenseSubCategory.SubCategory',
    'filter' => Html::activeDropDownList(
        $searchModel, 
        'ExpenseSubCategoryId', 
        ArrayHelper::map(
            LstExpensesSubCategories::find()
            ->where(["=", 'ExpenseCategoryId', $searchModel->ExpenseCategoryId])
            ->asArray()
            ->orderBy(['SubCategory'=>SORT_ASC])
            ->all(),
            'ExpenseSubCategoryId', 
            'SubCategory'
        ),
        [
            'class'=>'form-control',
            'prompt' => 'Select a Sub-Category',
        ]
    ),
],

but I’d like to be dependent only in the event a value is entered for $searchModel->ExpenseCategoryId, otherwise display all the options. Similar to s criteria of

=$searchModel->ExpenseCategoryId OR $searchModel->ExpenseCategoryId IS NULL

What would be the way to achieve this?

Use andFilterWhere() https://www.yiiframework.com/doc/api/2.0/yii-db-querytrait#andFilterWhere()-detail

1 Like

InsaneSkull,

Thank you. That works beautifully. I didn’t realize there was where() vs andFiletrWhere().

Thank you for taking the time to help!