如何在这里使用 OR 条件

How to use OR condition here

我有一个tablerecords和另一个tablecategories

我想得到这个类别中的所有记录

$all_categories = '5,6,7,8';

所以我正在使用这个代码:

$query = $this->Records->find('all',[
    'contain' => ['Categories']
]);

if(!empty($search)){
    $query->where(['Records.title LIKE' => '%'.$search.'%']);
}

if(!empty($wilaya)){
    $query->where(['Records.adresse LIKE' => '%'.$wilaya.'%']);
}

if(!empty($cat)){
    $query->where(['Records.category_id =' => $cat]);
} else {
    $categories_array = explode(',',$all_categories);
    foreach($categories_array as $category) {
        $query->where(['Records.category_id =' => $category]);  
    }
}

当我使用它时,默认情况下我得到 AND 条件。

我怎样才能获得 OR-条件?

这应该有效:

$all_categories = '5,6,7,8';
$array=explode(',',$all_categories);
$query->where(['Records.category_id' => $array], ['Records.category_id' => 'integer[]']);

注意: 编辑答案以添加有关列数据类型的信息。没有这个在 CakePHP 3.x.

中将无法工作

这等于:

$all_categories = '5,6,7,8';
$array=explode(',',$all_categories);
$query->where(['Records.category_id IN' => $array]);

Automatically Creating IN Clauses

使用IN:

$all_categories = '5,6,7,8';
$categories_array = explode(',',$all_categories);
$query->where(['Records.category_id IN' => $categories_array]);