如何在OR条件中使用一个isNull表达式

huangapple go评论49阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月14日 06:23:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定