Yii2 - 为空时忽略“或”条件

我有这个工作正常的查询


$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();

但是当$model->instructor_name为空或空时,我得到空结果。所以我想忽略何时instructor_id为空


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


我怎么能做到这一点?


陪伴而非守候
浏览 218回答 2
2回答

慕哥6287543

你应该使用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();

慕标5832272

$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();或者添加以下内容&nbsp; ->andWhere(['<>', 'instructor_id', null])或者&nbsp;->andWhere('instructor_id IS NOT NULL')
打开App,查看更多内容
随时随地看视频慕课网APP