join and orwhere 以 AND 结束

join and orwhere ends to AND

我有以下代码:

$_duplicates = $this->find()
    ->innerJoin(
        ['c' => 'contacts'],    //alias
        [
            'Contacts.contactname != ' => '',
            'Contacts.id < c.id',
            'c.id > ' => 0
        ]
        )
    ->select(['Contacts.id', 'Contacts.contactname', 'Contacts.legalname',
              'c.id', 'c.contactname', 'c.legalname'])
    ->orWhere([
        'LEVENSHTEIN(Contacts.contactname, c.contactname) <= ' => $distance,
        'LEVENSHTEIN(Contacts.contactname, c.legalname) <= ' => $distance,
        'LEVENSHTEIN(Contacts.legalname, c.contactname) <= ' => $distance,
        'LEVENSHTEIN(Contacts.legalname, c.legalname) <= ' => $distance
        ]);
debug($_duplicates);

那里的调试给出了这个输出:

SELECT Contacts.id AS `Contacts__id`, Contacts.contactname AS `Contacts__contactname`, 
  Contacts.legalname AS `Contacts__legalname`, c.id AS `c__id`,
  c.contactname AS `c__contactname`, c.legalname AS `c__legalname` 
FROM contacts Contacts 
INNER JOIN contacts c 
ON (Contacts.contactname != :c0 AND Contacts.id < c.id AND c.id > :c1)   
WHERE (
   Contacts.active = :c2 
   AND (
      LEVENSHTEIN(Contacts.contactname, c.contactname) <= :c3 
      AND LEVENSHTEIN(Contacts.contactname, c.legalname) <= :c4 
      AND LEVENSHTEIN(Contacts.legalname, c.contactname) <= :c5 
      AND LEVENSHTEIN(Contacts.legalname, c.legalname) <= :c6
    )
  )

任何 ide 为什么我在 LEVENSHTEIN 电话中得到 AND-s 而不是 OR? orWhere 应该在那里创建 OR 关系,对吗?

因为这不是 orWhere() 应该如何工作,OR 条件被用于与之前通过 where/andWhere/orWhere() 定义的条件相结合,即

->where(['a' => 'b'])
->orWhere(['c' => 'd', 'e' => 'f'])

结果

(c = d AND e = f) OR a = b

如果您需要通过 OR 组合所有条件,那么您可以使用多个 orWhere() 调用

->orWhere(['a' => 'b'])
->orWhere(['c' => 'd'])
->orWhere(['e' => 'f'])

OR

->where([
    'OR' => [
        'a' => 'b'
        'c' => 'd'
        'e' => 'f'
    ]
])

或表达式

->where(function (\Cake\Database\Expression\QueryExpression $exp) {
    return
        $exp->or_([
            'a' => 'b'
            'c' => 'd'
            'e' => 'f'
        ]);
})

另见 Cookbook > Database Access & ORM > Query Builder > Advanced Conditions