I'm trying to implement simple search functionality to my CakePHP application. I want users to be able to search based on a members first name, last name and the skills the member has.
My models are associated like this:
User ---hasOne---> Profile ---HABTM---> Skill
So a user has one profile, and a profile can have many skills and a skill can belong to many profiles. These relationships are set up correctly.
I've managed to get a basic first name search working by doing this:
public function memberList() {
if(isset($this->request->data['User']['query'])) {
// The user is searching, change the conditions of the retrieval
$q = $this->request->data['User']['query'];
$this->paginate = array(
'conditions' => array('Profile.firstname LIKE' => "%$q%")
);
}
// Retrieve member list
$this->set('users', $this->paginate('User'));
}
However, there are two things I need help with. One is how to do an OR
in the query. Because I want to search for the firstname and lastname, I need to do Profile.firstname LIKE %$q% OR Profile.lastname LIKE %$q%
. How can I achieve that with the conditions
parameter of the pagination call?
The second problem is searching by a users skills. Because Skill
has a relationship with Profile
instead of User
, I don't get any skills returned by the find call to my User
model. Is there a way I can change this so a user's profile's skills are returned? And if I get that far, how am I going to combine that with the firstname and lastname search without calling $this->paginate
twice?
Thank you for any help!