英文:
How to use an isNull expression in an OR condition
问题
Cakephp版本:4.3.5
我正在尝试构建一个使用isNull表达式的Or条件,该条件在自定义查找器中使用。
在文本中,我试图这样做:
public function findMyTasks(Query $query, array $options): object
{
$query
->where([
'Tasks.status' => $options['status'],
'Tasks.user_id' => $options['user_id'],
'OR' => [
['Tasks.contact_id' => null], // 这里是条件
['Contacts.status' => 1]
],
]);
return $query;
}
如果我想在没有OR的情况下在条件中使用isNull,我会使用以下代码:
->where(function (QueryExpression $exp, Query $q) {
return $exp->isNull('Tasks.contact_id');
})
我尝试过各种组合,参考了高级条件页面这里,但无法将表达式与条件结合而不出现语法错误。
问题:
如何构建使用isNull和OR条件的查找器。
谢谢,
Zenzs。
英文:
Cakephp Version: 4.3.5
I'm trying to construct an Or condition that uses an isNull expression in a custom finder.
In text I am trying to do this:
public function findMyTasks(Query $query, array $options): object
{
$query
->where([
'Tasks.status' => $options['status'],
'Tasks.user_id' => $options['user_id'],
'OR' => [
['Tasks.contact_id' => null], // HERE IS THE CONDITION
['Contacts.status' => 1]
],
]);
return $query;
}
If I wanted to use isNull in a condition without OR I would use the following:
->where(function (QueryExpression $exp, Query $q) {
return $exp->isNull('Tasks.contact_id');
})
I have tried various combinations by referencing the advanced condition page here but I cannot combine the expression with the condition without a syntax error.
Question:
How can I construct this finder that uses isNull with an OR condition.
Thanks,
Zenzs.
答案1
得分: 1
如文档所述:
'OR' => [
['Tasks.contact_id IS' => null],
['Contacts.status' => 1]
],
在页面稍下方有更多相关信息:
https://book.cakephp.org/4/en/orm/query-builder.html#automatic-is-null-creation
英文:
As documented:
'OR' => [
['Tasks.contact_id IS' => null],
['Contacts.status' => 1]
],
Just a bit further down on that page:
https://book.cakephp.org/4/en/orm/query-builder.html#automatic-is-null-creation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论