CakePHP中的复杂搜索无法正常工作

时间:2014-07-10 11:33:12

标签: sql cakephp search associations has-many

我在CakePHP中遇到searching功能的大问题。实际上,我认为CakePHP不是问题,问题是逻辑。我想我找不到完美的逻辑。

我正在使用CakePHP 2.3.6。我有一些Models,它们与hasMany关联相关。

现在,我的搜索表单包含以下字段:

Name, Gender, Country, Experience, Skills, Subject, Result.

我们假设所有这些都是文本字段。

现在,我想要的是,当用户搜索具有这些条件的用户时,所有这些条件都必须填满。我的意思是,我想要Name="Max" AND Gender="Male" AND Country="USA" AND Experience="5 Years" AND Skills="Programming" AND Subject="CSE" AND Result="4.51"

的用户

下面,

Model1:

Name, Gender, Country,.........

Model2:

Skills, Subject, Result,...........

Model3:

Exprience, .......

Model1 hasMany Model2
Model1 hasMany Model3

所以,到目前为止我尝试的是:

if($this->request->is('post')){
   $data=$this->request->data;
   if(!empty($data['skills']) || !empty($data['subject']) || !empty($data['result'])){
      $education=array();
      if(!empty($data['skills']))
         $education['AND']['Model2.skills']=$data['skills'];
      if(!empty($data['subject']))
         $education['AND']['Model2.subject']=$data['subject'];
      if(!empty($data['result']))
         $education['AND']['Model2.result >= ']=$data['result'];
      $bind['Model2']['conditions']=$education;
      $bind['Model2']['fields']=array('Model2.result','Model2.skills','Model2.subject');
   }
   if(!empty($data['experience'])){
      $bind['Model3']['conditions']['Model3.experience']=$data['experience'];
      $bind['Model3']['fields']=array('Model3.experience');
   }
   $personal=array();
   if(!empty($data['name']))
      $personal['AND']['Model1.name']=$data['name'];
   if(!empty($data['gender']))
      $personal['AND']['Model1.gender']=$data['gender'];
   if(!empty($data['district']))
      $personal['AND']['Model1.country']=$data['country'];
   $this->Model1->bindModel(array('hasMany'=>$bind));
   $this->paginate['Model1']['conditions']=$personal;
   $this->paginate['Model1']['fields']=array('Model1.id','Model1.name');
   $result=$this->paginate('Model1');
}

它为我提供了一些与搜索无关的数据,意思是,我希望满足所有条件,没有OR,一切都将是ANDed

那么,这里有什么问题?我该怎么办 ?请帮助我,这是一种紧急情况。

感谢。

1 个答案:

答案 0 :(得分:0)

确定,

我想最后我得到了解决方案。我想我应该在这里使用JOIN query for hasMany association