Cakephp 3如何在多个匹配之间添加OR WHERE子句? (深层联想)

时间:2016-11-24 18:56:03

标签: php mysql cakephp cakephp-3.0 cakephp-3.3

我的问题是当前的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是做我想要的最好方法吗?我对建议非常开放。我已经找了好几天了。

请帮忙!

0 个答案:

没有答案