我想在foreach循环中执行此查询,以使用elemant或Where Requette中的表(取决于数组元素的数量)。
foreach ( $mesDeparts as $mesDepart)
{
$holidays_rh_test = holidaystate::find()
->leftJoin('holidays', '`holidays`.`id` = `holidayState`.`holiday_id`')
->leftJoin('user', '`user`.`id` = `holidays`.`user_id`')
->leftJoin('space_membership', '`user`.`id` = `space_membership`.`user_id`')
// ->where(['holidayState.user_id'=>0, 'holidayState.user_position_id'=>0, 'holidayState.stat'=>'2'])
->where(['space_membership.space_id'=>$mesDepart->space_id])
->andFilterWhere(['or',['holidayState.user_id'=>Yii::$app->user->id,'holidayState.user_position_id'=>$user_position->id ],['holidayState.user_id'=>0, 'holidayState.user_position_id'=>0, 'holidayState.stat'=>'2']])
->orderBy('holiday_id DESC')
->all();
}
答案 0 :(得分:0)
您正在重复查询
您应该构建一个数组并使用in子句
foreach ( $mesDeparts as $key =>$mesDepart) {
$my_array[$key] = $mesDepart->space_id;
}
$holidays_rh_test = holidaystate::find()
->leftJoin('holidays', '`holidays`.`id` = `holidayState`.`holiday_id`')
->leftJoin('user', '`user`.`id` = `holidays`.`user_id`')
->leftJoin('space_membership', '`user`.`id` = `space_membership`.`user_id`')
->andFiilterWhere(['in', 'space_membership.space_id', $my_array])
->orderBy('holiday_id DESC')
->all();