Jump to content

[CakePHP] Searching 'like' in two fields


sanfly

Recommended Posts

Hi

 

I have two fields in my users table: first_name & last_name

 

I want to be able search by name (first or last or first & last) to return a result

 

eg: lets say I have Joe Bloggs and Joe Jenkins

 

If I search 'Joe' i get both back (thats fine)

If I search 'Bloggs' I just get Joe Bloggs back (also fine)

But if I search 'Joe Bloggs' I dont get the correct results back, I guess because its looking firstly in first_name and then in last_name

 

This is my request for this search

 

$u = $this->User->find('all', array('conditions' => array(
								"OR" => array(
										'User.first_name LIKE ' => "%" . trim($input) . "%", 
										'User.last_name LIKE ' => "%" . trim($input) . "%", 
										)
							) ));

 

I have an awful feeling this could get really complicated with exploding the input into arrays and nested requests?  Anyone have any ideas - especially ones that will keep it uncomplicated

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.