Yii2 使用 where 和 or 查询

问题描述

我正在为这个而苦苦挣扎,我尝试了各种组合,但都没有得到我想要的。 所以我的sql代码

select * from table where organisation_id`= org_id 
AND due_at` <= start_date 
AND due_at` >= end_date 
AND category_id = cat_id
and ((standard_task_template_id is null) or (standard_task_template_id not in (standardQuery))

我的 yii2 代码

$query = TaskEventAllocation::find()
->where([TaskEventAllocation::tableName().'.organisation_id'=> $organisation_id])
           ->andWhere(['<=','due_at',$unix_end_date->getTimestamp()])
        ->andWhere(['>=',$unix_start_date->getTimestamp()])
$query->andWhere(['tc.id' => $cat_or_group_id]);

然后如果我添加

$query->andWhere('and',['is','standard_task_template_id',new \yii\db\Expression('null')],['or',['not in',$standardTaskIds]]);

它回来了

SELECT `t_task_event_allocation`.* 
FROM `t_task_event_allocation` 
 WHERE (`t_task_event_allocation`.`organisation_id`=:qp3) 
AND (`due_at` <= :qp4) 
AND (`due_at` >= :qp5) 
AND (`group_id` != :qp6) A
ND (`tc`.`id`=:qp7) 
AND (and) 

我试过了

$query->andWhere('or',['and',$standardTaskIds]]);

产生与上面相同但在括号中或在括号中

$query->andWhere(['is','or',$standardTaskIds]);

产生一个错误,说参数 2 必须是数组类型

解决方法

->andWhere([
    'or',['standard_task_template_id' => null],['not in','standard_task_template_id',$standardTaskIds]
]);

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...