我的问题是当前的Cakephp 3.3框架,我尝试在两个Compartment
之间进行搜索,但每个框架都有不同的Equipement。
每个Cable
有2个Equipement
,每个Equipement
有1个Compartment
。如果我希望在2 Compartement
之间搜索OR条款,我该如何处理?
这是我的代码:
$cables = TableRegistry::get('cable_schedule');
if (isset($this->request->query['search'])){
$data = $this->request->query;
$query = $cables->find('all')->contain(['CableType', 'EquipementSource.Compartment', 'EquipementSource.System', 'EquipementDest.Compartment', 'EquipementDest.System'])
->where(['Cable_Mark like' => '%'.$data['cable_mark'].'%',
'OR' => [
['EquipementSource.Description like' => '%'.$data['equipement'].'%'],
['EquipementDest.Description like' => '%'.$data['equipement'].'%']
]])
->matching('EquipementSource.Compartment', function($q) use ($data){
return $q->where(['Compartment.Description like' => '%'.$data['compartment'].'%'])->orWhere(['Compartment.Navis_ID like' => '%'.$data['compartment'].'%']);
})
->matching('EquipementDest.Compartment2', function($q) use ($data){
return $q->where(['Compartment2.Description like' => '%'.$data['compartment'].'%'])->orWhere(['Compartment2.Navis_ID like' => '%'.$data['compartment'].'%']);
})
->matching('EquipementSource.System', function($q) use ($data){
return $q->where(['System.Description like' => '%'.$data['system'].'%']);
});
}
数据库结构全部在belongsTo
。
请注意,我添加了Compartment2
,它与Compartment
位于同一个表中,因为如果我为2个不同的匹配添加Compartement
两次,则别名是相同的所以它只搜索第一个(EquipementSource.Compartment
)。
目前,此查询在AND子句中搜索Compartment
,但我想要OR子句。
matching
是做我想要的最好方法吗?我对建议非常开放。我已经找了好几天了。
请帮忙!