0

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.

4
  • 1
    You might want to look into github.com/CakeDC/search and save yourself 30 lines of wild code :)
    – mark
    Commented Jul 10, 2014 at 11:45
  • Yeah, I tried that, but that is for Cake 2.5+ I think, and it gives me an error. That's why I am trying this now.@mark Commented Jul 12, 2014 at 3:56
  • Well, it should also work with cake2.3, but you should upgrade your code to current master anyway. that is highly recommended.
    – mark
    Commented Jul 12, 2014 at 10:52
  • Yeah, I have a plan to switch to the latest version, but for now I need a solution for this version, you know, changing version in the middle of a project is not easy. By the way, I think I got a solution, I think I should've used JOIN query for hasMany association here. I used it, and I think it gives me the result I want. Now I am just trying to get data for another complex condition.@mark Commented Jul 13, 2014 at 5:08

1 Answer 1

0

Ok,

I think at last I got the solution to this. I think I should've used JOIN query for hasMany association here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.