I have the following
$clients = ArrayHelper::map(
Clients::find()
->orderBy(['Company' => SORT_ASC])
->where(['Active' => 1])
->orWhere(['ClientId' => $model->ClientId])
->all(),
'ClientId',
'Company'
);
but the orWhere only applies if $model->ClientId has a value, it could be null. So how do I build this properly?
I’ve done
if (isset($model->ClientId)) {
$clients = ArrayHelper::map(
Clients::find()
->orderBy(['Company' => SORT_ASC])
->where(['Active' => 1])
->orWhere(['ClientId' => $model->ClientId])
->all(),
'ClientId',
'Company'
);
} else {
$clients = ArrayHelper::map(
Clients::find()
->orderBy(['Company' => SORT_ASC])
->where(['Active' => 1])
->all(),
'ClientId',
'Company'
);
}
but I’m sure there’s a better way, so I thought I’d ask and learn.
Thank you