I am in a big problem with searching
feature, in CakePHP. Actually, I think CakePHP is not the problem here, problem is the logic. I think I don't find the perfect logic for this.
I am using CakePHP 2.3.6. I have some Models
, which are related by hasMany
association.
Now, my search form has these fields :
Name, Gender, Country, Experience, Skills, Subject, Result.
Let's assume that all of them are textfields.
Now, what I want is, when a user search for users with these criteria, all of these criteria must be full-filled. I mean, I want the users whose Name="Max" AND Gender="Male" AND Country="USA" AND Experience="5 Years" AND Skills="Programming" AND Subject="CSE" AND Result="4.51"
.
Here,
Model1:
Name, Gender, Country,.........
Model2:
Skills, Subject, Result,...........
Model3:
Exprience, .......
And,
Model1 hasMany Model2
Model1 hasMany Model3
So, what I tried so far is :
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');
}
It gives me some data that are not related to the search, means, I want all that conditions to met, there will be no OR
, everything will be ANDed
.
So, what is the problem here ? What should I do ? Please help me, this is kind of emergency.
Thanks.