0

I have this query which is working fine

$order_items= OrderItem::find()
->where(['location_id'=>$model->location_name,'instructor_id'=>$model->instructor_name])
->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();

But when $model->instructor_name is empty or null, I am getting Null result. So I want to ignore when instructor_id is Null when

'instructor_id'=>$model->instructor_name or instructor_id'=> Null

how I can do that?

Joshi
  • 2,539
  • 5
  • 24
  • 53

2 Answers2

1
$order_items= OrderItem::find()
->where(['location_id'=>$model->location_name,'instructor_id'=>$model->instructor_name])
->andWhere(['not', ['instructor_id' => null]])
->andwhere(['between', 'date', $model->from_date, $model->to_date ])->all();

Or add the following

  ->andWhere(['<>', 'instructor_id', null])

Or

 ->andWhere('instructor_id IS NOT NULL')
user206
  • 858
  • 1
  • 7
  • 15
1

You should use andFilterWhere()

$order_items = OrderItem::find()
   ->where(['location_id' => $model->location_name])
   ->andFilterWhere(['instructor_id' => $model->instructor_name])
   ->andwhere(['between', 'date', $model->from_date, $model->to_date ])
   ->all();
Insane Skull
  • 8,810
  • 9
  • 38
  • 59